Skip to content

Commit 5655451

Browse files
committed
Custom HashSet done
1 parent 08d0d61 commit 5655451

File tree

3 files changed

+144
-1
lines changed

3 files changed

+144
-1
lines changed

src/main/java/com/leetcode/arrays/RemoveDuplicates.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
* <p>
1616
* Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
1717
* <p>
18-
* It doesn't matter what you leave beyond the returned length.
18+
* NOTE: It doesn't matter what you leave beyond the returned length.
1919
*
2020
* @author rampatra
2121
* @since 2019-04-24
2222
*/
2323
public class RemoveDuplicates {
2424

2525
/**
26+
* This removes the duplicates from the array in-place.
27+
* <p>
2628
* Time complexity: O(n)
2729
* where,
2830
* n = no. of elements in the array
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.leetcode.hashtables;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
/**
8+
* Level: Easy
9+
* Problem Link: https://leetcode.com/problems/contains-duplicate/
10+
*
11+
* @author rampatra
12+
* @since 2019-04-24
13+
*/
14+
public class ContainsDuplicates {
15+
16+
public static boolean containsDuplicates(int[] nums) {
17+
Set<Integer> numSet = new HashSet<>();
18+
for (int num : nums) {
19+
if (!numSet.add(num)) {
20+
return true;
21+
}
22+
}
23+
return false;
24+
}
25+
26+
/**
27+
* Runtime: <a href="https://leetcode.com/submissions/detail/224724092/">5 ms</a>.
28+
*
29+
* @param nums
30+
* @return
31+
*/
32+
public static boolean containsDuplicatesWithoutSet(int[] nums) {
33+
Arrays.sort(nums);
34+
for (int i = 0; i < nums.length - 1; i++) {
35+
if (nums[i] == nums[i + 1]) {
36+
return true;
37+
}
38+
}
39+
return false;
40+
}
41+
42+
public static void main(String[] args) {
43+
System.out.println(containsDuplicates(new int[]{1, 2, 3, 1}));
44+
System.out.println(containsDuplicates(new int[]{1, 2, 3, 4}));
45+
46+
System.out.println(containsDuplicatesWithoutSet(new int[]{1, 2, 3, 1}));
47+
System.out.println(containsDuplicatesWithoutSet(new int[]{1, 2, 3, 4}));
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.leetcode.hashtables;
2+
3+
/**
4+
* Level: Learning Cards
5+
* Problem Link: https://leetcode.com/explore/learn/card/hash-table/182/practical-applications/1139/
6+
*
7+
* @author rampatra
8+
* @since 2019-04-24
9+
*/
10+
public class MyHashSet {
11+
12+
private final int SIZE = 10000;
13+
private final Entry[] entries;
14+
15+
class Entry {
16+
int key;
17+
Entry next;
18+
19+
Entry(int key) {
20+
this.key = key;
21+
}
22+
}
23+
24+
/**
25+
* Initialize your data structure here.
26+
*/
27+
public MyHashSet() {
28+
entries = new Entry[SIZE];
29+
}
30+
31+
public void add(int key) {
32+
if (contains(key)) return;
33+
34+
Entry newEntry = new Entry(key);
35+
int bucket = key % SIZE;
36+
37+
newEntry.next = entries[bucket];
38+
entries[bucket] = newEntry;
39+
}
40+
41+
public void remove(int key) {
42+
int bucket = key % SIZE;
43+
Entry entry = entries[bucket];
44+
45+
if (entry != null && entry.key == key) {
46+
entries[bucket] = entry.next;
47+
return;
48+
}
49+
50+
Entry curr = new Entry(0);
51+
curr.next = entry;
52+
53+
while (curr.next != null && curr.next.key != key) {
54+
curr = curr.next;
55+
}
56+
57+
if (curr.next != null) {
58+
curr.next = curr.next.next;
59+
}
60+
}
61+
62+
/**
63+
* Returns true if this set contains the specified element
64+
*/
65+
public boolean contains(int key) {
66+
int bucket = key % SIZE;
67+
Entry entry = entries[bucket];
68+
69+
while (entry != null) {
70+
if (entry.key == key) {
71+
return true;
72+
}
73+
entry = entry.next;
74+
}
75+
76+
return false;
77+
}
78+
79+
public static void main(String[] args) {
80+
MyHashSet set = new MyHashSet();
81+
set.add(1);
82+
set.add(2);
83+
set.add(3);
84+
System.out.println(set.contains(1));
85+
System.out.println(set.contains(2));
86+
set.remove(2);
87+
System.out.println(set.contains(2));
88+
System.out.println(set.contains(3));
89+
set.remove(3);
90+
System.out.println(set.contains(3));
91+
}
92+
}

0 commit comments

Comments
 (0)