Question - 26:
Find the Duplicate Number
Given an array of integers nums
containing n + 1
integers where each integer is in the range [1, n]
inclusive.
There is only one repeated number in nums
, return this repeated number.
Example 1:
Input: nums = [1,3,4,2,2]
Output: 2
Example 2:
Input: nums = [3,1,3,4,2]
Output: 3
First approach:
class Solution {
public 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 0;
}
}
Step-by-step implementation:
The method
findDuplicate
takes an array of integersnums
as an input and returns an integer.The method sorts the input array using the
Arrays.sort
method. Sorting the array is important because it brings all duplicate elements together in the sorted order, making it easier to identify them.The method then loops through the sorted array using a
for
loop that starts at the first element and goes up to the second-last element of the array (i.e.,nums.length-1
).In each iteration of the loop, the code checks if the current element is equal to the next element in the array (i.e.,
nums[i] == nums[i+1]
). If the elements are equal, that means we have found a duplicate, and the method returns that element (i.e.,return nums[i]
).If the loop completes without finding any duplicates, the method returns 0 (i.e.,
return 0
).
The time complexity of this algorithm is O(nlogn) because of the sorting operation. The space complexity is O(1) because the algorithm does not use any extra space.
Second approach:
class Solution {
public int findDuplicate(int[] nums) {
Map<Integer,Integer> resMap = new HashMap<>();
for(int num:nums){
if(resMap.get(num)!=null && resMap.get(num)==num)
return num;
else resMap.put(num,num);
}
return 0;
}
}
Step-by-step implementation:
The method
findDuplicate
takes an array of integersnums
as an input and returns an integer.The method creates a new hashmap called
resMap
withInteger
as key andInteger
as value to keep track of previously seen numbers.The method loops through each element in the input array using an enhanced
for
loop.For each element in the input array, the method checks if the element already exists as a key in the
resMap
using theget()
method. If the element already exists as a key in theresMap
, it means the current element is a duplicate. In that case, the method checks if the value corresponding to the key in the map is equal to the current element. If it is, then we have found the duplicate, and the method returns that element (i.e.,return num
). If not, we skip to the next element.If the current element is not a duplicate, the method adds it to the
resMap
with the element as both the key and the value (i.e.,resMap.put(num, num)
).If the loop completes without finding any duplicates, the method returns 0 (i.e.,
return 0
).
The time complexity of this algorithm is O(n) because it uses a hashmap to check if an element already exists, which takes constant time on average. The space complexity is also O(n) because the size of the resMap
can grow up to the size of the input array nums
.
Third approach:
class Solution {
public int findDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
for(int num:nums){
if(set.contains(num)) return num;
else set.add(num);
}
return 0;
}
}
Step-by-step implementation:
The
findDuplicate
method takes an integer arraynums
as an input and returns an integer as output.The method creates a new hash set called
set
withInteger
as element type.The method loops through each element in the input array using an enhanced
for
loop.For each element in the input array, the method checks if the element already exists in the
set
using thecontains()
method. If the element already exists in theset
, it means the current element is a duplicate, so the method returns that element (i.e.,return num
).If the current element is not a duplicate, the method adds it to the
set
using theadd()
method (i.e.,set.add(num)
).If the loop completes without finding any duplicates, the method returns 0 (i.e.,
return 0
).
The time complexity of this algorithm is O(n) because the contains()
and add()
methods of a hash set take constant time on average. The space complexity is also O(n) because the size of the set
can grow up to the size of the input array nums
.
Conclusion:
The second and third approaches have lower time complexity and higher space complexity than the first. Depending on the requirements, one of three ways may be selected.
Thanks for reading...Happy learning...😊