The Most frequent Number in an array

后端 未结 9 1608
粉色の甜心
粉色の甜心 2021-01-17 14:14

I have this Array i wrote a function MostFreq that takes an array of integers and return 2 values : the more frequent number in the array and its frequency check this code i

9条回答
  •  借酒劲吻你
    2021-01-17 14:55

    Assuming you can't use LINQ, I'd probably approach the algorithm like this:

    • Create Key/Value dictionary
    • Iterate your array, add a key the dictionary for each unique elem, increment the value each time that element is repeated.
    • Walk the dictionary keys, and return the elem with the highest value.

    This isn't a great solution but it is simple, ContainsKey is an O(1) lookup, so you'll be at most iterating your array twice.

提交回复
热议问题