Search code examples
javaarraysalgorithmarray-algorithms

Removing duplicate characters in a String array


I have a sample input like [q w e r r t] and I want to remove duplicates and print [q w e r t] with arrays. I don't see why the output is different for the below code snippet.

for(int j=0; j< array.length; j++) {
    for(int k=j+1; k< array.length; k++) {
        if(array[j] == array[k]) {
            continue;
        }
        System.out.print(array[j] + " ");
        j = k;
    }
}

Update: I wanted to use this logic for a sorted array. I used Arrays.sort(). I changed == to .equals() for Strings.

public static void main(String args[]) throws IOException {

    // Enter size of array and assert the type of input
    System.out.println("Enter size of array in integers");
    Scanner sc = new Scanner(System.in);
    while (!sc.hasNextInt()) {
        System.out.println("Please enter integers");
        sc.next();
    }
    ;

    // Accepting the values into the array and sorting them
    int demoInt = sc.nextInt();
    String[] array = new String[demoInt];
    String[] outputMarkers = new String[demoInt];
    System.out.println("Enter the values");
    for (int i = 0; i < array.length; i++) {
        Scanner scNum = new Scanner(System.in);
        array[i] = scNum.next();
        if (i == array.length - 1) System.out.println("Array is full");
    }
    Arrays.sort(array);
    System.out.printf("Sorted array is : %s", Arrays.toString(array));

    //Checking for duplicates //Sample: a a a s d f
    for (int j = 0; j < array.length; j++) {
        for (int k = j + 1; k < array.length; k++) {
            if (array[j].equals(array[k])) {
                continue; //returns to for loop with increment
            }
            System.out.print(array[j] + ". ");
            j = k;
        }
    }
}

Input: a a a d f Output: a d f


Solution

  • Your problem is that you are checking each character against all the characters after it. Imagine an array with no duplicates; once you get to the last character, you have printed out all the characters before it. But when j = array.length - 1, then k = array.length, and the second for loop does not run at all, and your last character will never be printed.

    Your code would always fail to print the last element correctly. The only case in which it would be correct is if your last element is a duplicate of a previous element, but not of the second-to-last element.

    Try this code instead:

    outerloop:
    for (int j = 0; j < array.length; j++) {
        for(int k = 0; k < j; k++) {
            if(array[j] == array[k]) {
                    continue outerloop;
            }
        }
        System.out.print(array[j] + " ");
    }
    

    The premise of the code is that it loops through each of the characters. If it matches any of the previous characters, the code skips that element and continues to the next one.

    EDIT: Looks like you edited the question for a sorted array instead. That means if the last element is a duplicate, it will be a duplicate of the element before it so we don't have to worry about the corner case in my previous block of code.

    for(int j=0; j< array.length; j++) {
        for(int k=j+1; k< array.length; k++) {
            if(array[j] == array[k]) {
                continue;
            }
            System.out.print(array[j] + " ");
            j = k;
        }
    }
    System.out.print(array[array.length-1] + " ");