Nanina
Nanina

Reputation: 117

Delete/Remove duplicates from 2D array

I'm trying to remove duplicates from a diagonal array. If you run my code you will understand better what I'm trying to do.

I want to remove duplicates from that helper array but my duplicate part od the code works for all numbers instead of that diagonal part. Also, please don't include answers with collections. Here is the part where i'm having trouble:

 boolean dup = false;

        for (int i = 0; i < array.length; i++) {
         for (int j = 0; j <array[i].length; j++) {
             if (i+j ==array.length-1){
                 if(i == j) {
                     System.out.println("Duplicate "+array[i][j]);
                 }
                 if(array[i] ==array[j]){
                     System.out.println("Duplicate "+array[i][j]);
                     dup = true;
                     break;
                 }
                 System.out.print(array[i][j] + " ");
             }

            else System.out.print(" " + " ");
        }
        System.out.println();
        }

And here is the whole thing:

class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter length of N array: ");

        int n = input.nextInt();
        System.out.print("Enter length of M array: ");
        int m = input.nextInt();

        intarray[][] = new int[n][m];

        Random rand = new Random();

        System.out.print("Random nums:");
        for (int i = 0; i <array.length; i++) {
            for (int j = 0; j <array[i].length; j++) {
                System.out.print("X[" +i+ "," +j +"]"+ "-->");
               array[i][j] = rand.nextInt(10);
            }
    }
        for (int i = 0; i <array.length; i++) {
            System.out.println();
        for (int j = 0; j <array[i].length; j++) {
            System.out.print(array[i][j] + " ");
        }
    }
     System.out.println();
     System.out.println("Numbers: ");



        boolean dup = false;

        for (int i = 0; i <array.length; i++) {
         for (int j = 0; j <array[i].length; j++) {
             if (i+j ==array.length-1){
                 if(i == j) {
                     System.out.println("Duplicate "+array[i][j]);
                 }
                 if(array[i] ==array[j]){
                     System.out.println("Duplicate "+array[i][j]);
                     dup = true;
                     break;
                 }
                 System.out.print(array[i][j] + " ");
             }

            else System.out.print(" " + " ");
        }
        System.out.println();
        }

    }
}

Upvotes: 0

Views: 895

Answers (1)

gwcoderguy
gwcoderguy

Reputation: 422

You have the right idea here to check if i == j but you check for duplicates outside of you if check. Also you're right to look for if niz[i] == niz[j]. The problem is that you're doing each of these checks separately. So you're getting all items on the diagonal with your i==j check, as well as all items that are duplicates with your niz[i] == n[j] check.

Basically, you need to combine these checks: if(i==j) and if(niz[i] == niz[j]) to check that its 1. on the diagonal and 2. is a duplicate.

Try using an if check like: if (i==j && niz[i] == niz[j])

Upvotes: 2

Related Questions