Find the number in the array that happened most times

For an integer array, I need to find which number happened the most times. I wrote an algorithm as shown below.

  • Use a card to store the number and number of times it happened.

    map<int, int>

    Key: represents the number value: represents the number of times the failure occurred.

  • Scan the input array and update the map with the number and number of events.
  • Iterate through the map from start to finish. Find the key for the maximum value of which is present. This key becomes the number that happened the most number of times.

I implemented the algorithm as shown below.

#include <iostream> 
#include <map>
using namespace std; 
int main()
{
    int a[10] = {1,2,3,2,1,3,2,4,1,1}; //Input array: hardcoded for testing
    map<int, int> m;

    for(int i=0;i<10;i++)
    {
        m[a[i]]++;  //Increment the value of key for counting occurances
    }

    int mostNumTimes = 0; 
    int number = -999; //-999 represents invalid number
    map<int,int>::iterator it = m.begin();
    for( ;it != m.end(); it++)  //Find the number which occurred 
    {                           //most number of times
        if(it->second > mostNumTimes)
        {
            mostNumTimes = it->second;
            number = it->first;
        }
    }
    if(number != -999)   //Print number and number of times it occurred
    {
        cout<<"Number: "<<number<<endl;
        cout<<"Number of times occured: "<<mostNumTimes<<endl;
    }
    else
    {
        cout<<"Input array is empty"<<endl;
    }
    return 0;
}

Output:

Number 1

Number of times: 4

: ? , , - - , . ?

. , . .

+3
5

. O (N Log N), - N std:: map ( ) ( N). , .

-, .

+4

( count int), . , .

map<int, int> m;
int currentMax = -999;
int maxCount = 0;
for(int i=0;i<10;i++)
{
    int updated = m[a[i]]++;  //Increment the value of key for counting occurances        
    updated++; // due to post increment 
    if (maxCount < updated) {
         maxCount = updated;
         currentMax = i;
    }
}

, , , . O (N), , . , , , , , , , - ? , O (N), , . , - log (n), , .

+8

, ...

{1,1,1,1,2,2,2,3,3,4,4}

currentValue , , ... .. ()

currentValue = 0;
currentCount = 0;
maxValue = 0;
maxCount = 0;

for(int value in array) {
  if(currentValue == value) {
    currentCount++;
  } else {
    // is this greater than max count
    if(currentCount > maxCount) {
      maxCount = currentCount;
      maxValue = currentValue;
    }

    // reset values
    currentValue = value;
    currentCount = 0;
  }
}

, maxValue , maxCount.

+1

-, -999. map.empty() .

, , . , () , int .

- :

map<int, int>::iterator it = m.find(i);
if (it != m.end())
    m.second++;
    if (m.second > mostTimes) {
      // reset mostTimes and maxNumber = m.first here
    }
} else {
    m[i] = 1;
}

O (n) , , max ( , n, ). , , , mostTimes maxNumbers , , , RAM. , , .

0

( ), , ? , ,

int i = 0;
int j = sizeof(a)/sizeof(int);
for(;i < j;i++, j--)
{
  if (a[i] == a[j])
  {
    update<map_t, 2>(m, a[i]);
  }
  else
  {
    update<map_t, 1>(m, a[i]);
    update<map_t, 1>(m, a[j]);
  }
}
// if array size is odd...
if (i == j)
    update<map_t, 1>(m, a[i]);

here updating is a simple function because i'm too lazy to type the same code ...

template <typename M, int DEF>
void update(M& m, int v)
{
  typename M::iterator it = m.find(v);
  if (it != m.end())
      it->second += DEF;
  else
  {
    m.insert(pair<int, int>(v, DEF));
  }
}

everything else remains unchanged, i.e. your code is good and only minor improvements are possible ...

0
source

Source: https://habr.com/ru/post/1769146/


All Articles