-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContainsDuplicate.java
35 lines (30 loc) · 995 Bytes
/
ContainsDuplicate.java
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
34
35
/*
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.
*/
import java.io.*;
import java.util.*;
public class ContainsDuplicate {
public static boolean containsDuplicate(int[] nums) {
if(nums.length <= 0) return false;
HashSet<Integer> hp = new HashSet<Integer>();
for(int i=0; i < nums.length; i++){
if(!hp.add(nums[i])) return true;
}
return false;
}
public static void dispAr(int[] nums) {
for(int i = 0;i < nums.length; i++){
System.out.print(nums[i] + " ");
}
System.out.println();
return;
}
public static void main(String[] args) {
int[] nums = new int[]{1, 2, 5, 8, 7, 3, 2, 0};
boolean res = containsDuplicate(nums);
System.out.println("array:");
dispAr(nums);
System.out.println("contains duplicate? " + res);
return;
}
}