-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainsDuplicate_217.java
More file actions
33 lines (28 loc) · 953 Bytes
/
Copy pathContainsDuplicate_217.java
File metadata and controls
33 lines (28 loc) · 953 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.leetcode.array;
import java.util.HashSet;
import java.util.Set;
/**
* Created by charles on 12/17/16.
* Given an array of integers, find if the array contains any duplicates.
* Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
*/
public class ContainsDuplicate_217 {
private Set<Integer> set = new HashSet<>();
public boolean containsDuplicate(int[] nums) {
if (nums == null || nums.length == 0) {
return false;
}
int len = nums.length;
for (int i = 0; i < len; i++) {
if (!set.add(nums[i])) {
return true;
}
}
return false;
}
public static void main(String[] args) {
ContainsDuplicate_217 c = new ContainsDuplicate_217();
int[] nums = {1,2,3,1};
System.out.println(c.containsDuplicate(nums));
}
}