704. Count Occurrences of Element in Array

EasyRecursionRecursion

Given an integer array nums and an integer target, return the number of times target appears in nums. You must implement the counting recursively without using any loops.

Input: An integer array nums and an integer target.

Output: Return an integer count.

Examples

Example 1
Input: [1,2,3,2,1], 2
Output: 2
Explanation: 2 appears at indices 1 and 3 -> count = 2.
Example 2
Input: [7,7,7,7], 7
Output: 4
Explanation: All four positions match.
Example 3
Input: [1,2,3], 5
Output: 0
Explanation: 5 never appears.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →