Removing duplicate strings from an array?

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

    Try this code to remove dup words:

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < wordList.length; i++) {
        boolean found = false;
        for (int j = i+1; j < wordList.length; j++) {
            if (wordList[j].equals(wordList[i])) {
                found = true;
                break;
            }
        }
        // System.out.printf("Checking: [%s]%n", wordList[i]);
        if (!found) {
            if (sb.length() > 0)
                sb.append(' ');
            sb.append(wordList[i]);
        }
    }
    System.out.printf("Unique: [%s]%n", sb);
    

提交回复
热议问题