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.