从2D数组中删除/删除重复项

时间:2022-05-12 21:33:26

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();
        }

    }
}

1 个解决方案

#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.

你有正确的想法,检查我是否= = j,但如果检查你在外面检查重复。你是否正确寻找niz [i] == niz [j]。问题是你要分别进行这些检查。因此,您使用i == j检查获得对角线上的所有项目,以及使用您的niz [i] == n [j]检查重复的所有项目。

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.

基本上,你需要结合这些检查:if(i == j)和if(niz [i] == niz [j])检查对角线上的1.和2.是重复的。

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

尝试使用if检查:if(i == j && niz [i] == niz [j])

#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.

你有正确的想法,检查我是否= = j,但如果检查你在外面检查重复。你是否正确寻找niz [i] == niz [j]。问题是你要分别进行这些检查。因此,您使用i == j检查获得对角线上的所有项目,以及使用您的niz [i] == n [j]检查重复的所有项目。

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.

基本上,你需要结合这些检查:if(i == j)和if(niz [i] == niz [j])检查对角线上的1.和2.是重复的。

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

尝试使用if检查:if(i == j && niz [i] == niz [j])