Removing duplicate strings from an array?

前端 未结 9 751
野的像风
野的像风 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:45

    If you are allowed to use Lists, you can define a generic method that does this fairly easily:

    public  T[] removeDuplicates(final T[] array) {
        List noDuplicates = new ArrayList();
        for (T arrayElem : array) {
            if (!noDuplicates.contains(arrayElem)) {
                noDuplicates.add(arrayElem);
            }
        }
        return (T[]) noDuplicates.toArray();
    }
    

提交回复
热议问题