Duplicate Finder
easySave
ArrayHash Table
Write a function that takes an array of integers and returns the first integer that appears more than once when the array is read from left to right. If no integer is repeated, return -1.
Example 1
Input
[ 2, 1, 3, 5, 3, 2 ]
Output
3
Explanation. The number 3 is the first number that appears more than once.
Example 2
Input
[ 1, 2, 3, 4 ]
Output
-1
Explanation. There are no duplicate numbers.
Example 3
Input
[ -1, -3, -4, -1 ]
Output
-1
Explanation. The number -1 is the first negative number that appears more than once.
Example 4
Input
[ 10, 10, 10 ]
Output
10
Explanation. The number 10 is immediately repeated.
Follow-up: Can you improve your solution to run in O(n) time complexity and O(1) space complexity by modifying the input array?
Constraints:
1. The input array may contain both positive and negative integers. 2. The size of the array is at least 1 and at most 10,000.
- Views
- 3