Removing duplicate strings from an array?

前端 未结 9 749
野的像风
野的像风 2021-01-24 06:54

How can I remove duplicate strings from a string array without using a HashSet?

I try to use loops, but the words not delete.

StringBuffer outString = ne         


        
9条回答
  •  执念已碎
    2021-01-24 07:24

    The best and most effective method is to suppose arr is the array that contains strings and can have duplicate values:

    Arrays.sort(arr);
    int l = 0;
    for (int a = 0; a < arr.length; a++) {
        if (a == arr.length - 1)
            l++;// its a unique value
        else if (!(a[a + 1].equals(arr[a])))
            l++;// its also a unique
    }
    String newArray[] = new String[l];
    l = 0;
    for (int a = 0; a < arr.length; a++) {
        if (a == arr.length - 1)
            newArray[l] = arr[a];
        else if (!(a[a + 1].equals(arr[a]))) {
            newArray[l] = arr[a];
            l++;
        }
    }
    

提交回复
热议问题