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