Count Element Occurrences
EasyPractice library · +10 XPhash mapcountingfrequency
Given a list of N integers, print the count of occurrences for each unique integer present in the list. The output should be sorted based on the unique integers themselves in ascending order.
Input
The first line contains an integer N, the number of elements. The second line contains N space-separated integers.
Output
Print N lines, each containing a unique integer from the input list followed by a space, and then its frequency count. The output should be sorted by the unique integers.
Constraints
1 <= N <= 1000. Each integer will be between -1000 and 1000.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
5 1 2 2 3 1
Output
1 2 2 2 3 1
The number 1 appears twice, 2 appears twice, and 3 appears once. Output is sorted by the numbers.
Example 2
Input
7 10 20 10 30 20 10 40
Output
10 3 20 2 30 1 40 1
Counts for 10 (3), 20 (2), 30 (1), and 40 (1). Sorted by number.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 2 2 3 1
Example 2 input
7 10 20 10 30 20 10 40