Skip to content

Commit 2703274

Browse files
committed
update 217.contains-duplicate.java
1 parent f928895 commit 2703274

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

217.contains-duplicate.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 (56.60%)
10+
* Total Accepted: 749.2K
11+
* Total Submissions: 1.3M
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+
41+
HashSet<Integer> set = new HashSet<Integer>();
42+
43+
for (int i = 0; i < nums.length; ++i) {
44+
if (set.contains(nums[i])) {
45+
return true;
46+
}
47+
set.add(nums[i]);
48+
}
49+
50+
return false;
51+
}
52+
}

0 commit comments

Comments
 (0)