frequency of elements in an array

help me find frequency of elements in an array

If value of elements are less, make another array. Set it to 0, and use-

frequency[array[i]]++;

If elements are large, then use map data structure in c++. BTW, its an easily googleable question, so please try googling first.

You can use an array of numbers if the range of elements is nearly 10^6 . Initialize the array with 0 . Increment the array position at index equal to the inputted number. This way you can store the frequency of each number . For example

long long int arr[100000]={0};
cin>>num;
arr[num]++;

Thing to note is you have to make a global array for the 10^6 elements.

Other method can be used when the range is greater than 10^6 . Use unordered_map to hash the values and update the mapped values accordingly .

http://www.cplusplus.com/reference/unordered_map/unordered_map/

1 Like

you beat by just by few second . :slight_smile:

Your ans is more detailed tho :).

BTW, its 2 min :stuck_out_tongue:

//