Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo,

hat jemand einen Tipp, wie man die häufigste Zahl, die in einem array auftritt, mittels eines zweiten arrays bestimmen kann?

Gruß

Hypo

ich gehe von c++ aus: bau dir nen map-container aus dem stl, schlüssel, ist die vorkommende zahl und inhalt ist die häufigkeit. geh mit ner schleife übers erste array und fülle deine map (falls zahl schon vorkommt, dann inkermentiere in der map an der entsprechenden stelle, wenn nicht, lege neues element an). danach machst du noch eine statistik mit ner max()-funktion und dann hast dus.

geht aber noch einfacher (sorry für den ersten vorschlag, bin extrem müd, aber lass ihn ma stehen ;) :

kopier das erste in ein gleichwertiges zweites array, sortier dieses, gehe es dann durch. da sie alle sortiert sind, kannst du, wenn du die häufigkeit der aktuellen zahl inkementierst, davon ausgehen, dass, sobald die nächste zahl kommt, die aktuelle danach nie mehr vorkommt. deshalb kannst du ne max-variable mit der anzahl füllen und sobald die aktuelle zahl ne größere häufigkeit hat, nimmt max diese an, sonst bleibt sie. dazu merkst du dir noch in ner zweiten variablen, welche zahl zu dieser max-variablen gehört.

gruß und ich entschuldige mich 1000x für fehlende groß-klein-schreibung :-(

benjamin

Mal quick & dirty nur ein Beispiel, das ein mögliches Prinzip zeigt.


#include <conio>
#include <iostream>

int main()
{
int i[10] = {1,2,2,2,2,4,4,5,6,7};
int anzahl[10] = {0,0,0,0,0,0,0,0,0,0};
int wert;
for(int z=0; z<10; z++)
{
wert = i[z];
anzahl[wert]++;
}
for(int z=0; z<10; z++)
cout << z << " kommt " << anzahl[z] << " mal vor " << endl;
getch();
return 0;
}
[/PHP]

Viel Erfolg, Theosoft

Hallo,

Mal quick & dirty nur ein Beispiel, das ein mögliches Prinzip zeigt.

Da hast Du aber massive Probleme, wenn in Deinem Array Zahlen auftreten die größer als das Array sind:

int i[10] = {1223,24543,20000,21222,20000,412345,412345,5000000,61,7};

Nic

Vielen Dank für Eure Tipps. Ich hab's hinbekommen. So als Übung habe ich die Zahlen auch nicht so groß gewählt bzw. mit dem Zufallsgenerator erstellt.

Gruß

Hypo

@nic_power

yep, mein Beispiel dachte ich für eiinstellige Zahlen ... und nur als Anregung zum weiterdenken :P

Kannste auch per count (STL) machen.


#include <algorithm>
int main()
{
int wert[10]={1,4,6,4,3,9,7,8,2,1};
int counter[10];
for(int i=1;i<=10;i++)
{
counter[i-1]=count(wert,wert+10,wert[i-1]);
cout << counter [i-1] << endl;
}
}
[/PHP]

  • 15 Jahre später...

Moin!

Das Array muss vorher sortiert werden. Ich habe dafür die Funktion sort() benutzt aus der Bibliothek <algorithms>. Man kann dafür auch einen eigenen Bubble-Sort verwenden.

Hier die Lösung:

#include <algorithm>
#define ANZ 100
using namespace std;

void main(){
  
int Werte[ANZ];
int zahl = 0;
int temp = -1;
int counter = 0;

  sort(Werte, Werte + ANZ);
  
	for (int i = 0; i < ANZ; i++){
		zahl = Werte[i];
		if (temp != zahl){
			for (int y = 0; y < Anzahl; y++){
				if (zahl == Werte[y]) counter++;
			}
			cout << zahl << " ist " << counter << "x vorhanden." << endl;
			counter = 0;
		}
		temp = zahl;
	}
}

VG,

Der_Code

vor 2 Minuten schrieb KeeperOfCoffee:

Na hoffentlich wird ihm das nach 16 Jahren weiterhelfen

Außerdem ist der Thread auf der ersten Seite und da kein Code hier funktioniert hat....Vielleicht freut sich ja jemand, wenn er nach einer Lösung sucht.

VG

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.