#K39242. Count Distinct Elements and Their Occurrences
Count Distinct Elements and Their Occurrences
Count Distinct Elements and Their Occurrences
You are given an array of n integers. Your task is to count the number of distinct integers in the array and, for each distinct integer, output the number of times it appears in the array in the order of its first appearance.
Mathematically, let \( arr = [a_1, a_2, \dots, a_n] \). You need to determine the distinct count \( d \) and a list \( L \) such that if an element \( x \) appears in \( arr \) for the first time at position \( i \), and it appears \( m \) times overall, then \( L \) will contain \( m \) at the corresponding position.
Input: The first line contains an integer \( n \) (the number of elements). The second line contains \( n \) space-separated integers.
Output: The first line should print the number of distinct integers. The second line should print the occurrence counts of each distinct integer in the order of their appearance, separated by a space.
inputFormat
Input Format:
- The first line contains an integer \( n \), the number of elements in the array.
- The second line contains \( n \) space-separated integers representing the array.
outputFormat
Output Format:
- Output the number of distinct integers in the first line.
- Output the list of occurrence counts corresponding to each distinct integer in the order of their first appearance in the second line, with each count separated by a space.
7
4 2 4 5 2 3 5
4
2 2 2 1
</p>