1. 程式人生 > >java 判斷一個數組是否有重複值

java 判斷一個數組是否有重複值

import java.util.HashSet;

public class test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] array = { 1, 2, 3, 4, 6, 6, 7, 8, 9 };
        if (cheakIsRepeat(array)) {
            System.out.println("沒有重複值!");
        } else {
            System.out.println("有重複值!");
        }
    }

    /*
     * 判斷陣列中是否有重複的值
     */
    public static boolean cheakIsRepeat(int[] array) {
        HashSet<Integer> hashSet = new HashSet<Integer>();
        for (int i = 0; i < array.length; i++) {
            hashSet.add(array[i]);
        }
        if (hashSet.size() == array.length) {
            return true;
        } else {
            return false;
        }
    }
}
--------------------- 
作者:半夜不睡覺 
來源:CSDN 
原文:https://blog.csdn.net/wh01096045/article/details/49451979 
版權宣告:本文為博主原創文章,轉載請附上博文連結!