0

I have an array whose input is given by the user by using keypad. All the inputs are stored in an array named storeKeys. The index of the array is held by a variable storeKeysIndex is initialized to 0 and when a key is pressed, the value of pressed key is stored in storeKeys array and the value of storeKeysIndex is incremented. Let the storeKeys have values

storeKeys = {3,2,5,7,1,1,9,9,9,1,3,1};

I want the lcd to display the top three repeated elements in the array. In above array, lcd should display 1,9,3 since value '1' is repeated four times in the above array, '9' is repeated three times, '3' is repeated two times. Other values should not be displayed by lcd.

I have tried to count the frequency of each element and store the frequencies in an array freq. I can display the frequency of each element in the serial monitor. But I'm not getting how to display the top three repeated numbers on the lcd (or in serial monitor). Here is the code:

for(int i=0; i<12; i++)
{
    freq[i] = -1;
}
for(int i=0; i<12; i++)
{
    count = 1;
    for(int j=i+1; j<12; j++)
    {
        if(storeKeys[i] == storeKeys[j])
        {
            count++;
            freq[j] = 0;
        }
    }

    if(freq[i]!=0)
    {
        freq[i] = count;
    }
}
for(int i=0; i<12; i++)
{
    if(freq[i]!=0 && storeKeys[i] != NULL)
    {
        Serial.println(storeKeys[i]);
        Serial.println(freq[i]);
        Serial.println("==========");
    }
}

The above code works but displaying frequency of all elements. I want to display the top three repeated elements only.

Here is the link for simulator online:

https://circuits.io/circuits/5073094-the-unnamed-circuit/edit

Thanks.

Sri Harsha
  • 148
  • 1
  • 8
  • [WT........](https://stackoverflow.com/questions/44363166/something-is-wrong-with-my-deletion) – LPs Jun 05 '17 at 09:14
  • What is meant by "top three repeated elements"? The most repetitions, the highest digit value, the highest index? That needs to be clear for the cases where there are more than 3 repeated digits. In your example, they are shown in the order encountered. – Weather Vane Jun 05 '17 at 09:23

2 Answers2

1

The answer is from here

Sort your frequency array with the below function and then only take the last three elements of sorted array.

 void sort(int a[], int size)
 {
   for(int i=0; i<(size-1); i++) 
   {
    for(int o=0; o<(size-(i+1)); o++) 
       {
            if(a[o] > a[o+1]) 
            {
                int t = a[o];
                a[o] = a[o+1];
                a[o+1] = t;
            }
        }
    }

  }
user_4685802
  • 155
  • 9
  • Thanks for the reply. The frequency array just contains the frequency of the values of **storeKeys** array. See the link and run the code in the link. – Sri Harsha Jun 05 '17 at 11:20
0

I got the answer! Thanks for the help

for (int i = 0; i < 10; i++)  // Loop to sort the storeKeys array according to freq array
{   
   if(freq[i]!=0 && storeKeys[i] != NULL){
    for (int j = i + 1; j < 10; j++)
    {
        if (freq[i] < freq[j])
        {
            a = freq[i];
            freq[i] = freq[j];
            freq[j] = a;
            b = storeKeys[i];
            storeKeys[i] = storeKeys[j];
            storeKeys[j] = b;
        }
    }
   }
}
for(int i=0;i<10;i++) {  
  if(storeKeys[i] != NULL && freq[i] != 0){
    // Serial.println(storeKeys[i]);
    while(c<3){
      mostPressed[c] = storeKeys[i];
      c=c+1;
      break;
    }
  }
  if(c==3){
    break;
  }
}

Here I sorted the storeKeys array according to the frequency array and stored the top three results in a new array. I got it myself. Thanks for the help!

Sri Harsha
  • 148
  • 1
  • 8