Removing duplicate strings from an array?

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

    Try this...

    public class RemoveDupsStringArray {
    
    public static void main(String[] args) {
        String[] withDuplicates = new String[] {"one","one","two","three","one","three","three"};
        String[] withoutDuplicates = new String[] {"one","two","three"};
    
        removeDuplicates(withDuplicates);
        removeDuplicates(withoutDuplicates);
    }
    
    private static void removeDuplicates(String[] array) {
        int[] occurence = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            for(int j=i+1;j

提交回复
热议问题