0

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();
 }
 }
}
asked Jun 5, 2017 at 18:51
3
  • Run your code under debugger step-by-step and you will be able to inspect all your variables in real time. Commented Jun 5, 2017 at 18:54
  • 1
    If you run my code you will understand what's going on. Or you could tell us what's going on. Put effort into your question. Commented Jun 5, 2017 at 18:57
  • I did include the explanation as well. You could put effort into reading everything before you say something. @Jashaszun Commented Jun 5, 2017 at 19:31

1 Answer 1

2

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])

answered Jun 5, 2017 at 19:00

1 Comment

Thank you! That helps!! Would you know how to remove them now when I have correct loop?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.