Find Duplicate in Array
Given an array of integers where each integer is between 1 and n (inclusive), where 'n' is the length of the array, write a function that finds and returns the integer that appears more than once in the array. Assume there is exactly one integer that meets this condition.
[ 3, 1, 3, 4, 2 ]
Explanation. The number 3 appears twice in the array.
[ 1, 2, 2, 3, 4 ]
Explanation. The number 2 appears twice in the array.
[ 1, 1 ]
Explanation. The number 1 appears twice in the array, which are the only two numbers present.
[ 5, 4, 3, 2, 5, 1 ]
Explanation. The number 5 appears twice in the array.
Follow-up: Can you implement your solution in O(n) time complexity and O(1) space complexity by modifying the input array?
The input array will have a length of at least 2 and at most 10,000. Each integer in the array is in the range from 1 to n, where n is the size of the array.
- Views
- 2