Skip to content

Commit

Permalink
add 1429
Browse files Browse the repository at this point in the history
  • Loading branch information
fishercoder1534 committed Dec 24, 2021
1 parent c915d76 commit 532fba0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ _If you like this project, please leave me a star._ ★
|1436|[Destination City](https://leetcode.com/problems/destination-city/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1436.java) | |Easy|String|
|1432|[Max Difference You Can Get From Changing an Integer](https://leetcode.com/problems/max-difference-you-can-get-from-changing-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1432.java) | |Medium|String|
|1431|[Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1431.java), [C++](../master/cpp/_1431.cpp) | |Easy|Array|
|1429|[First Unique Number](https://leetcode.com/problems/first-unique-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1429.java) | |Medium|Array, HashTable, Design, Data Streams|
|1428|[Leftmost Column with at Least a One](https://leetcode.com/problems/leftmost-column-with-at-least-a-one/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1428.java) | |Medium|Array|
|1427|[Perform String Shifts](https://leetcode.com/problems/perform-string-shifts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1427.java) | |Easy|Array, Math|
|1426|[Counting Elements](https://leetcode.com/problems/counting-elements/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1426.java) | |Easy|Array|
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1429.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fishercoder.solutions;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

public class _1429 {
public static class Solution1 {
/**
* Credit: https://leetcode.com/problems/first-unique-number/discuss/602698/Java-Easy-Set-O(1)
* <p>
* LinkedHashSet is a handy data structure to help preserve the order of all unique elements.
*/
public static class FirstUnique {

Set<Integer> uniqSet;
Set<Integer> nonUniqSet;

public FirstUnique(int[] nums) {
uniqSet = new LinkedHashSet<>();
nonUniqSet = new HashSet<>();
for (int num : nums) {
add(num);
}
}

public int showFirstUnique() {
if (!uniqSet.isEmpty()) {
return uniqSet.iterator().next();
}
return -1;
}

public void add(int value) {
if (uniqSet.contains(value)) {
uniqSet.remove(value);
nonUniqSet.add(value);
}
if (!nonUniqSet.contains(value)) {
uniqSet.add(value);
}
}
}
}
}

0 comments on commit 532fba0

Please sign in to comment.