Skip to content

Commit 7993a12

Browse files
committed
solve 217.contains-duplicate
1 parent 91f3155 commit 7993a12

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

vscode/217.contains-duplicate.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @lc app=leetcode id=217 lang=java
3+
*
4+
* [217] Contains Duplicate
5+
*
6+
* https://leetcode.com/problems/contains-duplicate/description/
7+
*
8+
* algorithms
9+
* Easy (50.84%)
10+
* Total Accepted: 322K
11+
* Total Submissions: 625.4K
12+
* Testcase Example: '[1,2,3,1]'
13+
*
14+
* Given an array of integers, find if the array contains any duplicates.
15+
*
16+
* Your function should return true if any value appears at least twice in the
17+
* array, and it should return false if every element is distinct.
18+
*
19+
* Example 1:
20+
*
21+
*
22+
* Input: [1,2,3,1]
23+
* Output: true
24+
*
25+
* Example 2:
26+
*
27+
*
28+
* Input: [1,2,3,4]
29+
* Output: false
30+
*
31+
* Example 3:
32+
*
33+
*
34+
* Input: [1,1,1,3,3,4,3,2,4,2]
35+
* Output: true
36+
*
37+
*/
38+
class Solution {
39+
public boolean containsDuplicate(int[] nums) {
40+
Set<Integer> set = new HashSet<Integer>();
41+
for (int num : nums) {
42+
if (set.contains(num)) {
43+
return true;
44+
}
45+
set.add(num);
46+
}
47+
return false;
48+
}
49+
}
50+

0 commit comments

Comments
 (0)