mediumArraysPattern: Array Search
Duplicate Element Index Solution
Problem Statement
You are given an array of integers `elements` containing at least one duplicate. Find the index of the first occurrence of a duplicate element in the array.
Examples
Example 1:
Input:{"elements":[2,1,3,5,3,2]}
Output:3
Explanation: the first occurrence of duplicate 3 is at index 3
Example 2:
Input:{"elements":[4,5,7,5,9]}
Output:1
Explanation: the first occurrence of duplicate 5 is at index 1
Example 3:
Input:{"elements":[1,2,3,4,5,6,7,8,9,1]}
Output:0
Explanation: the first occurrence of duplicate 1 is at index 0
Constraints
- The input array will not be empty.
- Each page object will have a unique id and some data.
- The array length will not exceed 10000.
Time: O(n) Space: O(n)
Analyze constraints and compute optimal solutions step-by-step.
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
No solution code is currently loaded.
Complete this code in the workspace editor.
