如何在数组数组中找到独特的数组?

发布于 2025-01-20 06:55:34 字数 350 浏览 3 评论 0原文

我正在尝试检查数组数组是否有重复值。最终目标将是一个 if 语句,内容是:

if (arr 没有重复值) {继续代码}

int [][] arr = new int[3][2];
int [] x = new int[1];
arr[0][0] = 1;
arr[0][1] = 1;
arr[1][0] = 1;
arr[1][1] = 2;
arr[2][0] = 1;
arr[2][1] = 2;


x = arr.getUnique(); /I assume it'll use getUnique() but I can't even get that to work

任何帮助都会很棒!谢谢

I'm trying to check whether an array of arrays has any duplicate values. The end goal would be an if statement saying:

if (arr does NOT have duplicate values) {continue code}

int [][] arr = new int[3][2];
int [] x = new int[1];
arr[0][0] = 1;
arr[0][1] = 1;
arr[1][0] = 1;
arr[1][1] = 2;
arr[2][0] = 1;
arr[2][1] = 2;


x = arr.getUnique(); /I assume it'll use getUnique() but I can't even get that to work

Any help would be awesome! Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情仇皆在手 2025-01-27 06:55:34

Java 有一个 HashSet 您可以使用有一个没有重复的集合。

找到一种方法将数组转换为 HashSet 并比较它们的元素数量。如果它们具有相同数量的元素,则数组中的每个元素都是唯一的,如果 HashSet 较小,则意味着一些重复项已被删除。

Java has a HashSet you can use to have a collection without duplicates.

Find a way to convert your array into a HashSet and compare the number of their elements. If they have the same number of elements, each element in your array is unique, if the HashSet is smaller, it means some duplicates have been removed.

心凉 2025-01-27 06:55:34

您可以创建自己的辅助函数,如下所示:

private static boolean arrayHasDuplicates(int[][] arr) {
        for (int i = 0; i < arr.length; i++)
        {
            for (int y = 0; y < arr.length; y++)
            {
                if (arr[i].length != arr[y].length) continue;
                if (i == y) continue;
                boolean same = true;
                
                for (int x = 0; x < arr[y].length; x++)
                {
                    if (arr[i][x] != arr[y][x]) {
                        same = false;
                        break;
                    }
                }
                if (same) return same;
            }
        }
        return false;
    }

You could create a helper function of your own like this:

private static boolean arrayHasDuplicates(int[][] arr) {
        for (int i = 0; i < arr.length; i++)
        {
            for (int y = 0; y < arr.length; y++)
            {
                if (arr[i].length != arr[y].length) continue;
                if (i == y) continue;
                boolean same = true;
                
                for (int x = 0; x < arr[y].length; x++)
                {
                    if (arr[i][x] != arr[y][x]) {
                        same = false;
                        break;
                    }
                }
                if (same) return same;
            }
        }
        return false;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文