579.Count Occurrences of a Value
Easy
Given an array
nums and a target, return how many times the target appears.
Counting combines a linear scan with a counter. Unlike searching, it can never stop early: every element might be another match, so an unsorted array always needs one full pass. (If the array were sorted, all copies would sit together and binary searchBinary SearchRepeatedly halving a sorted range to find a value in O(log n) steps. could find their range much faster.)
Example 1:
Input: nums = [3,7,3,1,3], target = 3
Output: 3
Example 2:
Input: nums = [3,7], target = 5
Output: 0
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 1000 - ●
-1000000 ≤ nums[i], target ≤ 1000000
nums =
[3, 7, 3, 1, 3]
target =
3