Skip to content

Commit

Permalink
Solution to find Duplicates in array
Browse files Browse the repository at this point in the history
  • Loading branch information
oyerounak committed Oct 7, 2023
1 parent 3f6dccc commit 12f8d14
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Medium/287. Find Duplicate Number/New Approach/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.Arrays;
public class Solution {
public int findDuplicate(int[] nums) {
Arrays.sort(nums);

for(int i=1;i<nums.length;i++){
if(nums[i]==nums[i-1])
return nums[i];
}
return -1;
}

}
13 changes: 13 additions & 0 deletions Medium/287. Find Duplicate Number/New Approach/sol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Simplest and Easiest approach to solve

1. Sort the input array `nums` in ascending order.

2. Initialize a loop from `i` = 1 to `n` (inclusive), where `n` is the length of the sorted `nums` array.

3. Within the loop:

a. Check if `nums[i]` is equal to `nums[i-1]`.

b. If the condition is true, return `nums[i]` as it is the duplicate element.

4. If no duplicate is found after the loop completes, return -1 to indicate that there are no duplicates in the array.

0 comments on commit 12f8d14

Please sign in to comment.