733. First Occurrence of Element in Array

EasyRecursionRecursion

Given an integer array nums and an integer target, return the index of the FIRST occurrence of target in nums, or -1 if it does not appear. The array may contain duplicates. You must implement the search recursively, scanning from index 0.

Input: An integer array nums and an integer target.

Output: Return an integer: the smallest index where target appears, or -1.

Examples

Example 1
Input: [1,2,3,2,1], 2
Output: 1
Explanation: First 2 appears at index 1.
Example 2
Input: [5,5,5,5], 5
Output: 0
Explanation: First (and every) occurrence is at index 0.
Example 3
Input: [1,2,3], 4
Output: -1
Explanation: 4 is not present.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →