The Most frequent Number in an array

后端 未结 9 1612
粉色の甜心
粉色の甜心 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:16

    int count = 1;
    int currentIndex = 0;
    for (int i = 1; i < A.Length; i++)
    {
        if (A[i] == A[currentIndex])
            count++;
        else
            count--;
        if (count == 0)
        {
            currentIndex = i;
            count = 1;
        }
    }
    
    int mostFreq = A[currentIndex];
    

提交回复
热议问题