To print the frequency of the elements in an array, I have solved but facing some print issues. Help to resolve

  array-algorithms, arrays, c++, frequency, sorting

https://www.hackerrank.com/contests/cp-tutorial/challenges/cp-tutorial-11/problem
below is my solution But the problem I am having is that the output prints the statement exactly the no of times the element is repeated. Can somebody help me out with this.enter image description here

#include <iostream>
#include <algorithm>
using namespace std;``
int main()
{
   int n;
    cin >> n;
    int a[1000005];

    for(int i=0;i<n;i++)
        cin >> a[i];

    sort(a, a + n);
    for(int j=0;j<n;j++)
    {   
        int count=0;
        for(int k =0;k<n;k++)
        {
            if(a[j] == a[k])
            {
                count = count +1;
            }
        }

if(count==1)
        {
            cout << a[j] << " appears once in the array." << 'n';
        }
        if(count>1)
        {
            cout << a[j] << " appears " << count << " times in the array." << 'n';
       
        }
    }
}

Source: Windows Questions C++

LEAVE A COMMENT