Count Number Occurrences
EasyPractice library · +10 XPhash mapcountingfrequency
Given a list of N integers, count the occurrences of each distinct integer in the list. Print each distinct integer and its count, sorted by the integer value.
Input
The first line contains an integer N. The second line contains N space-separated integers.
Output
Print N lines. Each line contains a distinct integer followed by its count, separated by a space. The output should be sorted by the distinct integers in ascending order.
Constraints
1 <= N <= 1000. Each integer will be between -10^9 and 10^9.
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
5 1 2 1 3 2
Output
1 2 2 2 3 1
The number 1 appears twice, 2 appears twice, and 3 appears once. Sorted output.
Example 2
Input
7 -5 0 -5 10 0 0 10
Output
-5 2 0 3 10 2
Handles negative numbers and zeros. Output is 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 1 3 2
Example 2 input
7 -5 0 -5 10 0 0 10