C# :: Aufgabe #102 :: Lösung #1
10 Lösungen

#102
Das Häufigste Element in einem Array
Anfänger - C#
von klhlubek19
- 16.07.2015 um 13:46 Uhr
Schreiben Sie ein Programm, das aus einem Array das häufigste Element findet. Sollte es mehrere gleicher Anzahl finden, so darf irgend ein Element dieser Häufigsten ausgegeben werden.
#1

von DBqFetti (2480 Punkte)
- 17.07.2015 um 12:49 Uhr

using System; using System.Collections.Generic; using System.Linq; namespace Most_in_Array { class Program { static void Main() { Random random = new Random(); int[] array = new int[15]; for(int i = 0; i < array.Length; i++) array[i] = random.Next(4); Dictionary<int, int> counter; counter = array.Distinct().ToDictionary(x => x, x => 0); foreach(int value in array) { counter[value]++; Console.Write("{0} ", value); } Console.WriteLine("\n{0}", counter.First(x => x.Value == counter.Values.Max()).Key); Console.ReadKey(true); } } }
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1