Integer Counter
easySave
ArrayCountingHash Table
Given an array of integers, calculate the number of times each integer appears in the array. Return the results as a string in the form of number:count, separated by commas in the order of their first appearance.
Example 1
Input
[ [ 1, 2, 2, 3 ] ]
Output
1:1,2:2,3:1
Explanation. The integer 1 appears 1 time, 2 appears 2 times, and 3 appears 1 time. The order reflects their first appearance.
Example 2
Input
[ [ 4, 4, 4, 4 ] ]
Output
4:4
Explanation. The integer 4 appears 4 times.
Example 3
Input
[ [ 8, 8, 10, 10, 8 ] ]
Output
8:3,10:2
Explanation. 8 is the first integer appearing 3 times followed by 10 which appears 2 times.
Follow-up: How would you optimize this solution if the input array is very large?
Constraints:
The array will contain at least one integer and all elements will be valid integers.
- Views
- 1