how to remove find the mode of an array in c#?
i know there are a lot of threats of this question but non of them has
help me the way i want. so here is my problem: i have this array: int []
array={1,1,1,2,2,4} i want to show the mode of that array but when i do
it, it shows three ones and one two and it should be one 1 and one 2, i
want to save the 2 numbers (1 and 2) in a list box and the number 4 in
another list box i just started programming (first semester) so i don't
know a lot of it
i found this code but i didn't fully understand it i don't know how to use
a dictionary and i can not use it because i do not know how it works sorry
for my bad English
int[] numsArr = { 1, 1, 1, 2, 2, 3, 4, 5, 6 };
Dictionary<int, int> dic = new Dictionary<int, int>();
for (int i = 0; i < numsArr.Length; i++)
{
if (dic.ContainsKey(numsArr[i]))
{
dic[numsArr[i]] = ++dic[numsArr[i]];
}
else
dic.Add(numsArr[i], 1);
}
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<int, int> kvp in dic)
sb.AppendLine(String.Format("Number {0} has {1} repetitions.",
kvp.Key, kvp.Value));`enter code here`
Console.WriteLine(sb.ToString());
Console.ReadLine();
No comments:
Post a Comment