Number Frequency Counter

Easy
0/40
0 upvote
Asked in company
Amdocs

Problem statement

You are given a list of N integers. Your task is to count the frequency of each unique number in the list.

You must then print the unique numbers and their corresponding frequencies, sorted in ascending order by the number.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer 'N', the number of elements in the list.
The second line contains 'N' space-separated integers, representing the list of numbers.


Output Format:
For each unique number found in the list, print a new line in the format: number->frequency
The lines must be sorted in ascending order based on the number.


Note:
The numbers can be positive, negative, or zero.
Sample Input 1:
8
2 4 2 8 4 4 2 9


Sample Output 1:
2->3
4->3
8->1
9->1


Explanation for Sample 1:
The frequencies of the numbers are:
2 appears 3 times.
4 appears 3 times.
8 appears 1 time.
9 appears 1 time.
The output is printed in ascending order of the numbers (2, 4, 8, 9).


Sample Input 2:
5
10 20 30 40 50


Sample Output 2:
10->1
20->1
30->1
40->1
50->1


Explanation for Sample 2:
All numbers in the list are unique, so each has a frequency of 1.


Expected Time Complexity:
The expected time complexity is O(N log K) or O(N), where N is the number of elements and K is the number of unique elements.


Constraints:
1 <= N <= 10^5
-10^9 <= number <= 10^9

Time limit: 1 sec
Approaches (1)
Number Frequency Counter
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Number Frequency Counter
Full screen
Console