Removing duplicate strings from an array?

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

    How about using a List:

    wordList = outString.toString().split(", ");
    List finalList = new ArrayList();
    for(String val : wordList) {
      if(!finalList.contains(val)) {
        finalList.add(val);
      }
    }
    

    A Set would be more efficient, however. If you can't use a List or a Set, and you are forced to remove the duplicates, then you will have to loop through the array each time, which will perform horribly.

提交回复
热议问题