Count Occurrences (Set 4)

Easy

Given an array of integers nums and a target, return how many times target appears in the array.

Count Occurrences (Set 4) — the data. This set runs 4 cases: nums holds arrays of 18–27 items, with values from 1 to 5; target ranges over 2–5. The hidden cases are the largest of the set.

Example 1

countOccurrences()
Input
nums = [2, 3, 2, 4, 4, 5, 5, 1, 2, 5, 5, 3, 2, 1, 1, 3, 3, 2], target = 5
Output
4

Why

`countOccurrences` returns `4` for this input.

Example 2

countOccurrences()
Input
nums = [4, 1, 5, 1, 2, 4, 4, 2, 4, 2, 5, 3, 5, 4, 5, 2, 2, 1, 5, 3, 1, 2], target = 4
Output
5

Why

`countOccurrences` returns `5` for this input.
Constraints
1 <= nums.length <= 10^5
-10^4 <= nums[i], target <= 10^4
Every case in this set must pass: the first two are visible on Run, the last two unlock on Submit.

Topics

ArraysCounting

Implement countOccurrences in the editor and return the answer — printing it is never graded.