💡 Sorting the array makes duplicates adjacent, simplifying detection. This approach is faster but modifies the input, which may not be allowed.
Intuition
Sort the array so duplicates appear next to each other, then scan once to find the duplicate.
Algorithm
- Sort the array in ascending order.
- Iterate through the sorted array.
- Compare each element with the next one.
- Return the element if it equals the next element.
💡 Sorting reduces the problem to a simple linear scan for duplicates.
def findDuplicate(nums):
nums.sort()
for i in range(len(nums) - 1):
if nums[i] == nums[i + 1]:
return nums[i]
return -1
# Driver code
if __name__ == '__main__':
print(findDuplicate([3,1,3,4,2])) # Output: 3
Line Notes
nums.sort()Sort the array to bring duplicates together
for i in range(len(nums) - 1):Iterate through array except last element
if nums[i] == nums[i + 1]:Check adjacent elements for duplicates
return nums[i]Return the duplicate immediately
import java.util.Arrays;
public class Solution {
public static int findDuplicate(int[] nums) {
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
return nums[i];
}
}
return -1;
}
public static void main(String[] args) {
int[] nums = {3,1,3,4,2};
System.out.println(findDuplicate(nums)); // Output: 3
}
}
Line Notes
Arrays.sort(nums);Sort the array to group duplicates
for (int i = 0; i < nums.length - 1; i++) {Iterate through array except last element
if (nums[i] == nums[i + 1]) {Check adjacent elements for duplicates
return nums[i];Return duplicate immediately
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int findDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end());
for (int i = 0; i < (int)nums.size() - 1; i++) {
if (nums[i] == nums[i + 1]) {
return nums[i];
}
}
return -1;
}
int main() {
vector<int> nums = {3,1,3,4,2};
cout << findDuplicate(nums) << endl; // Output: 3
return 0;
}
Line Notes
sort(nums.begin(), nums.end());Sort array to bring duplicates together
for (int i = 0; i < (int)nums.size() - 1; i++) {Iterate through array except last element
if (nums[i] == nums[i + 1]) {Check adjacent elements for duplicates
return nums[i];Return duplicate immediately
function findDuplicate(nums) {
nums.sort((a, b) => a - b);
for (let i = 0; i < nums.length - 1; i++) {
if (nums[i] === nums[i + 1]) {
return nums[i];
}
}
return -1;
}
// Test
console.log(findDuplicate([3,1,3,4,2])); // Output: 3
Line Notes
nums.sort((a, b) => a - b);Sort array numerically to group duplicates
for (let i = 0; i < nums.length - 1; i++) {Iterate through array except last element
if (nums[i] === nums[i + 1]) {Check adjacent elements for duplicates
return nums[i];Return duplicate immediately
TimeO(n log n)
SpaceO(1) or O(log n) depending on sorting implementation
Sorting dominates time complexity; scanning is linear.
💡 For n=100000, sorting takes about 1.5 million operations, which is feasible but modifies input.
Interview Verdict: Accepted if modification allowed
Good improvement but not allowed if input must remain unchanged.