The Most frequent Number in an array

后端 未结 9 1604
粉色の甜心
粉色の甜心 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 15:21

    You could eliminate the sort you do at the start by iterating the entire array once, keeping a count of how many times you come across each value in a temporary array, and then iterating the temporary array for the highest number. You could keep both the highest frequency count and the most frequent item throughout, too.

    Different sorts have different efficiencies on different types of data, of course, but this would be a worst case of just two iterations.

    Edit: Apologies for the repeat... 'Tweren't there when I started :)

提交回复
热议问题