题目:
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.
链接: http://leetcode.com/problems/contains-duplicate/
2/25/2017, Java
1 public class Solution { 2 public boolean containsDuplicate(int[] nums) { 3 if (nums.length == 0) return false; 4 HashSet<Integer> s = new HashSet<Integer>(); 5 6 for(int i = 0; i < nums.length; i++) { 7 if (s.contains(nums[i])) return true; 8 s.add(nums[i]); 9 } 10 return false; 11 } 12 }