Count Occurrences (Set 2)

Easy

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

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

Example 1

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

Why

`countOccurrences` returns `3` for this input.

Example 2

countOccurrences()
Input
nums = [5, 5, 4, 4, 1, 1, 4, 5, 1, 3, 1, 1, 4, 5, 3, 4], 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.