Removing duplicate strings from an array?

前端 未结 9 740
野的像风
野的像风 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);
    
    0 讨论(0)
  • 2021-01-24 07:21

    In your inner loop, initialize j = i + 1

    if(wordlist[i] != null && wordlist[i].equals(worldlist[j])) { wordlist[j] = null; }
    

    ...and then compact the array when you're finished to remove all null values

    0 讨论(0)
  • 2021-01-24 07:23

    1) I think you need to use the equals operator. Try

    if (!wordList[i].equals(wordList[j])){
    

    instead of !=.

    2) Also Kevin is right. You need to set t back to false.

    3) Side note as pointed out by others already: To be more efficient you should start the inner loop with

    for (j = i+1; j < wordList.length; j++) {
    

    4) Another side note: Your result array is still too long. If you don't want to use a List<String> and it is ok to loose the original array you could go with a solution as suggested by Zim-Zam O'Pootertoot and set the original duplicates to null, add a counter to count how many null values you assigned, initialize the new array with the correct size and loop a final time over the first array and copy only the non-null values into your new array.

    0 讨论(0)
  • 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++;
        }
    }
    
    0 讨论(0)
  • 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<array.length;j++){
                if(array[i]==array[j]){
                    occurence[j]=j;
                }
            }
        }
        int resultLength=0;
        for(int i=0;i<occurence.length;i++){
            if(occurence[i]==0){
                resultLength++;
            }
        }
        String[] result=new String[resultLength];
        int index=0;int j=0;
        for(int i=0;i<occurence.length;i++){
            index = occurence[i];
            if(index==0){
                result[j]= array[i];
                j++;
            }
        }
    
        for(String eachString : result){
            System.out.println(eachString);
        }
    }
    }
    
    0 讨论(0)
  • 2021-01-24 07:35

    Iterate through the array, and store in an auxiliary int[] or List<Integer> the indexes of duplicates that you find with your two for's.

    Create a new Array, with size equal to the original one minus the size of the repeated Strings.

    Iterate through your original array, if the index isn't on your auxiliary list, set it to your new Array.

    0 讨论(0)
提交回复
热议问题