First Recurring Element

Easy~10 min

Given an array of integers, return the first element that appears more than once as you scan from left to right.

If no element recurs, return -1.

Important: "First recurring" means the first element where you encounter a duplicate as you read left to right, not the element with the smallest index in the original array.

Examples

Example 1
Input: nums = [2, 5, 1, 2, 3, 5]
Output: 2
Explanation: When we reach the second 2 at index 3, it is the first duplicate we encounter.
Example 2
Input: nums = [1, 2, 3, 4, 5]
Output: -1
Explanation: No element appears more than once.
Example 3
Input: nums = [3, 1, 3, 1]
Output: 3
Explanation: The second 3 at index 2 is encountered before the second 1 at index 3.

Constraints

  • 0 <= nums.length <= 10^4
  • -10^5 <= nums[i] <= 10^5
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run