580.Count How Often Each Element Appears
Easy
Given an array
nums, return an array of the same length where each position holds how many times that position's value appears in nums.
Frequency countingFrequency CountingRecording how many times each distinct value occurs, usually in a hash map from value to count. is one of the most useful techniques in programming. Rather than recounting a value every time it is needed, count everything once into a hash mapHash MapA structure that stores key → value pairs and can look up a key in O(1) time on average. and look the answers up afterwards — turning an O(n²) approach into O(n).
Example 1:
Input: nums = [5,2,5,8,5,2]
Output: [3,2,3,1,3,2]
Example 2:
Input: nums = [7]
Output: [1]
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 1000 - ●
-1000000 ≤ nums[i] ≤ 1000000
nums =
[5, 2, 5, 8, 5, 2]