First and Last Occurence of an Element in an Array Problem LeetCode

Click to go to First and Last Occurence of an Element in an Array Problem in LeetCode

1. Initialize an array `returnArray` with default values `{ -1, -1 }` to be returned if the target is not found.

2. Search for the starting index of the target in the array:

- Initialize `start to -1.

- Iterate through the array using a for loop.

- If the current element is equal to the target, set `start` to the current index and break out of the loop.

3. If the starting index (`start`) is still -1 after the loop, it means the target is not present in the array. In this case, return `returnArray`.

4. Search for the ending index of the target: - Initialize `end` to the starting index (`start`). - Iterate from the starting index to the end of the array using another for loop. - Update the `end` index whenever the target is found. - Break the loop when a different element is encountered.

5. Update the `returnArray` with the starting and ending indices found in steps 2 and 4.

6. Return the `returnArray`.

Click for My Solution in Github
Leetcode Menu