ここでは、配列の重複した値をチェックする方法を示すJavaの例を添付しました。私はこれを実装する2つの方法を示します。

1)2つのforループを使って配列の各値を比較する – Line 21

2)HashSetを使用して重複した値を検出する。 – 40行目

Arrayの重複した値を比較する他の方法が分かっていれば、私にそれを教えてください。

package com.mkyong;

import java.util.Set;
import java.util.HashSet;

public class CheckDuplicate
{
    public static void main(String args[])
    {
        String[]sValue = new String[]{"a","b","c","d","","","e","a"};

        if(checkDuplicated__withNormal(sValue))
            System.out.println("Check Normal : Value duplicated! \n");
        if(checkDuplicated__withSet(sValue))
            System.out.println("Check Set : Value duplicated! \n");

    }

   //check duplicated value
    private static boolean checkDuplicated__withNormal(String[]sValueTemp)
    {
        for (int i = 0; i < sValueTemp.length; i++) {
            String sValueToCheck = sValueTemp[i];
            if(sValueToCheck==null || sValueToCheck.equals(""))continue;//empty ignore
            for (int j = 0; j < sValueTemp.length; j++) {
                    if(i==j)continue;//same line ignore
                    String sValueToCompare = sValueTemp[j];
                    if (sValueToCheck.equals(sValueToCompare)){
                            return true;
                    }
            }

        }
        return false;

    }

   //check duplicated value
    private static boolean checkDuplicated__withSet(String[]sValueTemp)
    {
        Set sValueSet = new HashSet();
        for(String tempValueSet : sValueTemp)
        {
            if (sValueSet.contains(tempValueSet))
                return true;
            else
                if(!tempValueSet.equals(""))
                    sValueSet.add(tempValueSet);
        }
        return false;
    }


}

リンク://タグ/配列/[配列]リンク://タグ/複製/[複製]リンク://タグ/java/[java]