1 public static boolean cheakRepeat(int[] array){ 2 HashSet<Integer> hashSet = new HashSet<Integer>(); 3 for (int i = 0; i < array.length; i++) { 4 hashSet.add(array[i]); 5 } 6 if (hashSet.size() == array.length){ 7 return true; 8 }else { 9 return false; 10 } 11 }
由於hashset 實現了set接口,所以它不允許集合中有重復的值,在調用add方法時,如果插入了重復值,會返回false。
hashset的更多特性可以看這篇博客http://www.cnblogs.com/chenjfblog/p/7522158.html