Skip to content

Commit 96d1af6

Browse files
committed
Add contains duplicate algorithm
1 parent 2416fd3 commit 96d1af6

File tree

8 files changed

+173
-30
lines changed

8 files changed

+173
-30
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ List of Programs related to data structures and algorithms
3434

3535
### Array
3636

37-
1. Contains duplicates : [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/containsDuplicate.js
38-
)
37+
1. Contains duplicates : [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/containsDuplicate/containsDuplicate.js) [Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/containsDuplicate/containsDuplicate.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/containsDuplicate/containsDuplicate.md)
38+
3939
2. Product of array except self: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/productExceptSelf/productExceptSelf.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/productExceptSelf/productExceptSelf.md)
4040

4141
3. Max sum subarray: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/maxSubArray.js)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package containsDuplicate;
2+
import java.util.*;
3+
4+
class ContainsDuplicate {
5+
private static boolean containsDuplicate(int[] nums) {
6+
HashSet<Integer> set = new HashSet<>();
7+
8+
for (int num : nums) {
9+
if (set.contains(num)) {
10+
return true;
11+
}
12+
set.add(num);
13+
}
14+
return false;
15+
}
16+
17+
private static boolean containsDuplicateUsingMap(int[] nums) {
18+
HashMap<Integer, Integer> map = new HashMap<>();
19+
20+
for (int num : nums) {
21+
if (map.containsKey(num)) {
22+
return true;
23+
}
24+
map.put(num, 1);
25+
}
26+
return false;
27+
}
28+
29+
public static void main(String[] args) {
30+
int[] nums1 = new int[] { 8, 6, 4, 2, 6 };
31+
int[] nums2 = new int[] { 1, 3, 5, 7, 9 };
32+
33+
System.out.println(containsDuplicate(nums1));
34+
System.out.println(containsDuplicate(nums2));
35+
36+
System.out.println(containsDuplicateUsingMap(nums1));
37+
System.out.println(containsDuplicateUsingMap(nums2));
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
**Problem statement:**
2+
Given an integer array `nums`, return `true` if any value appears at least twice in the array, and return `false` if every element is distinct.
3+
4+
## Examples:
5+
Example 1:
6+
7+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
8+
Output: true
9+
10+
Example 2:
11+
12+
Input: nums = [1,2,3,4, 5]
13+
Output: false
14+
15+
**Algorithmic Steps(Approach 1&2)**
16+
This problem is solved with an optimal solution using either set or map to find out duplicate elements exists or not. The algorithmic approach can be summarized as follows:
17+
18+
1. Create an empty set or map to store the elements.
19+
20+
2. Iterate an input array using for-each loop.
21+
22+
3. If the current element appears in a set or map, return `true` immediately to indicate duplicate elements exist.
23+
24+
4. Otherwise add the current element to the set or map.
25+
26+
5. After the for loop, return `false` to indicate there are no duplicate elements exist in the array.
27+
28+
29+
**Time and Space complexity:**
30+
This algorithm has a time complexity of O(n), where n is the number of elements in an array. This is because we are traversing the array at most once.
31+
32+
Here, we use any additional datastructure like set or map. Hence, the space complexity will be O(n).

src/java1/algorithms/array/twoSum2/TwoSum2.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package twoSum2;
2-
3-
import java.util.Arrays;
2+
import java.util.*;
43

54
public class TwoSum2 {
65

src/java1/algorithms/stack/BlancedBrackets.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package java1.datastructures.stack;
1+
package java1.algorithms.stack;
22

33
import java.util.Scanner;
44
import java.util.Stack;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Early exit using set:- TC: O(n), SC: O(n)
2+
function containsDuplicate(nums) {
3+
let noDupsSet = new Set();
4+
for(const num of nums) {
5+
if(noDupsSet.has(num)) {
6+
return true;
7+
}
8+
noDupsSet.add(num);
9+
}
10+
return false;
11+
}
12+
13+
// Early exit using object:- TC: O(n), SC: O(n)
14+
function containsDuplicateUsingObject(nums) {
15+
let noDupsObj = {};
16+
for(const num of nums) {
17+
if(noDupsObj[num]) {
18+
return true;
19+
}
20+
noDupsObj[num] = true;
21+
}
22+
return false;
23+
}
24+
25+
//Use Set size:- TC: O(n), SC: O(n)
26+
function containsDuplicateUsingSize(nums) {
27+
return new Set(nums).size !== nums.length;
28+
}
29+
30+
//Using sort and iteration:- TC: O(n log n), SC: O(n)
31+
function containsDuplicateUsingSort(nums) {
32+
nums.sort((a, b) => a-b);
33+
for(let i =1; i< nums.length; i++) {
34+
if(nums[i] === nums[i-1]) {
35+
return true;
36+
}
37+
}
38+
return false;
39+
}
40+
41+
//Using naive/Brute force:- TC: O(n^2), SC: O(1)
42+
function containsDuplicateUsingBruteforce(nums) {
43+
for(let i =0; i< nums.length-1; i++) {
44+
for (let j = i+1; j < nums.length; j++) {
45+
if(nums[i] == nums[j]) {
46+
return true;
47+
}
48+
}
49+
}
50+
return false;
51+
}
52+
53+
console.log("-----Has duplicates----");
54+
let nums1 = [8, 6, 4, 2, 6];
55+
console.log(containsDuplicate(nums1));
56+
console.log(containsDuplicateUsingObject(nums1));
57+
console.log(containsDuplicateUsingSize(nums1));
58+
console.log(containsDuplicateUsingSort(nums1));
59+
console.log(containsDuplicateUsingBruteforce(nums1));
60+
console.log("-----No duplicates----");
61+
let nums2 = [1, 3, 5, 7, 9];
62+
console.log(containsDuplicate(nums2));
63+
console.log(containsDuplicateUsingObject(nums2));
64+
console.log(containsDuplicateUsingSize(nums2));
65+
console.log(containsDuplicateUsingSort(nums2));
66+
console.log(containsDuplicateUsingBruteforce(nums2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
**Problem statement:**
2+
Given an integer array `nums`, return `true` if any value appears at least twice in the array, and return `false` if every element is distinct.
3+
4+
## Examples:
5+
Example 1:
6+
7+
Input: nums = [1,1,1,3,3,4,3,2,4,2]
8+
Output: true
9+
10+
Example 2:
11+
12+
Input: nums = [1,2,3,4, 5]
13+
Output: false
14+
15+
**Algorithmic Steps(Approach 1&2)**
16+
This problem is solved with an optimal solution using either set or object to find out duplicate elements exists or not. The algorithmic approach can be summarized as follows:
17+
18+
1. Create an empty set or an object to store the elements.
19+
20+
2. Iterate an input array using for-each loop.
21+
22+
3. If the current element appears in a set or an object, return `true` immediately to indicate duplicate elements exist.
23+
24+
4. Otherwise add the current element to the set or an object.
25+
26+
5. After the for loop, return `false` to indicate there are no duplicate elements exist in the array.
27+
28+
29+
**Time and Space complexity:**
30+
This algorithm has a time complexity of O(n), where n is the number of elements in an array. This is because we are traversing the array at most once.
31+
32+
Here, we use any additional datastructure like set or object. Hence, the space complexity will be O(n).

src/javascript/algorithms/array/containsDuplicate.js

-25
This file was deleted.

0 commit comments

Comments
 (0)