Skip to content

Commit e9d11de

Browse files
committed
feat: add 217. Contains Duplicate
1 parent 5de0d48 commit e9d11de

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
*.zip
1919
*.tar.gz
2020
*.rar
21-
21+
*.DS_Store
2222
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2323
hs_err_pid*
24-
src/.DS_Store
25-
src/com/nphausg/.DS_Store

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,20 @@ Medium: https://medium.com/@nphausg
2828

2929
## 💎 Coding Challenges
3030

31-
| Challenge | Level | Java | Kotlin |
32-
|--------------------------------------------------------------------------------------------------------------------|--------|-----------------------------------------------------------------------------|-------------------------------------------------------------------|
33-
| [0001. Two sum](https://leetcode.com/problems/two-sum) | 🔥 | - | [Solution](src/com/nphausg/leetcode/easy/TwoSum.kt) |
34-
| [0020. ValidParentheses](https://leetcode.com/problems/valid-parentheses) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ValidParentheses.java) | - |
35-
| [0026. Remove Duplicates](https://leetcode.com/problems/remove-duplicates-from-sorted-array) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/RemoveDuplicates.java) | - |
36-
| [0027. Remove Element](https://leetcode.com/problems/remove-element) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/RemoveElement.java) | - |
37-
| [0121. Best time to buy and sell stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | 🔥 | - | [Solution](src/com/nphausg/leetcode/easy/BuyAndSellStock.kt) |
38-
| [1929. Concatenation Of Array](https://leetcode.com/problems/concatenation-of-array) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ConcatenationArray.java) | - |
39-
| [0017. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/LetterCombinations.java) | - |
40-
| [0091. Decode Ways ](https://leetcode.com/problems/decode-ways) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/DecodeWays.java) | - |
41-
| [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/LongestCommonSubsequence.java) |
42-
| [0658. Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/FindClosestElements.java) | - |
43-
| [0041. First Missing Positive](https://leetcode.com/problems/first-missing-positive) | 🔥🔥🔥 | [Solution](src/com/nphausg/leetcode/hard/FirstMissingPositive.java) | [Solution](src/com/nphausg/leetcode/hard/FirstMissingPositive.kt) |
31+
| Challenge | Level | Java | Kotlin |
32+
|--------------------------------------------------------------------------------------------------------------------|--------|---------------------------------------------------------------------------|-------------------------------------------------------------------|
33+
| [0001. Two sum](https://leetcode.com/problems/two-sum) | 🔥 | - | [Solution](src/com/nphausg/leetcode/easy/TwoSum.kt) |
34+
| [0020. ValidParentheses](https://leetcode.com/problems/valid-parentheses) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ValidParentheses.java) | - |
35+
| [0026. Remove Duplicates](https://leetcode.com/problems/remove-duplicates-from-sorted-array) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/RemoveDuplicates.java) | - |
36+
| [0027. Remove Element](https://leetcode.com/problems/remove-element) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/RemoveElement.java) | - |
37+
| [0121. Best time to buy and sell stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | 🔥 | - | [Solution](src/com/nphausg/leetcode/easy/BuyAndSellStock.kt) |
38+
| [0217. Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ContainsDuplicate.java) | - |
39+
| [1929. Concatenation Of Array](https://leetcode.com/problems/concatenation-of-array) | 🔥 | [Solution](src/com/nphausg/leetcode/easy/ConcatenationArray.java) | - |
40+
| [0017. Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/LetterCombinations.java) | - |
41+
| [0091. Decode Ways ](https://leetcode.com/problems/decode-ways) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/DecodeWays.java) | - |
42+
| [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/LongestCommonSubsequence.java) |
43+
| [0658. Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements) | 🔥🔥 | [Solution](src/com/nphausg/leetcode/medium/FindClosestElements.java) | - |
44+
| [0041. First Missing Positive](https://leetcode.com/problems/first-missing-positive) | 🔥🔥🔥 | [Solution](src/com/nphausg/leetcode/hard/FirstMissingPositive.java) | [Solution](src/com/nphausg/leetcode/hard/FirstMissingPositive.kt) |
4445

4546
![-----------------------------------------------------](https://raw.githubusercontent.com/andreasbm/readme/master/assets/lines/colored.png)
4647

src/com/nphausg/leetcode/.DS_Store

-6 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.nphausg.leetcode.easy;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
6+
/**
7+
* <a href="https://leetcode.com/problems/contains-duplicate">Contains duplicate</a>
8+
*/
9+
public class ContainsDuplicate {
10+
public boolean containsDuplicate(int[] nums) {
11+
for (int i = 0; i < nums.length - 1; i++) {
12+
for (int j = i + 1; j < nums.length; j++) {
13+
if (nums[j] == nums[i]) {
14+
return true;
15+
}
16+
}
17+
}
18+
return false;
19+
}
20+
21+
public boolean containsDuplicate2(int[] nums) {
22+
Arrays.sort(nums);
23+
for (int i = 1; i < nums.length; i++) {
24+
if (nums[i] == nums[i - 1]) {
25+
return true;
26+
}
27+
}
28+
return false;
29+
}
30+
31+
public boolean containsDuplicate3(int[] nums) {
32+
HashSet<Integer> set = new HashSet<>();
33+
for (int i = 1; i < nums.length; i++) {
34+
if (set.contains(nums[i])) {
35+
return true;
36+
}
37+
set.add(nums[i]);
38+
}
39+
return false;
40+
}
41+
42+
43+
}

0 commit comments

Comments
 (0)