如何在数组中查找重复元素的索引(Java)

huangapple go评论69阅读模式
英文:

How to Find Index of a Duplicate in An Array (Java)

问题

对于这个编程作业,我们需要找出数独谜题一行中数组中重复项的索引。我有这个方法,静态布尔型 equals(int[] a, int[] a2):

boolean isValue = true;
int[] dataRow = { 9, 8, 7,  6, 5, 4,  3, 2, 8 }; 
int[] chk = { 9, 8, 7,  6, 5, 4,  3, 1, 8 };
isValue = Arrays.equals(dataRow, chk);
int i, j;

for (i = 0; i < dataRow.length; i++) 
{
    for (j = 0; j < dataRow.length; j++) 
    {
        if (dataRow[i] == dataRow[j]) 
        {
            System.out.println("重复项 - " + dataRow[i] + " 在索引 " + i + " 和 " + j + " 找到");
            //--8 在索引 8 和 1 找到

这个程序只是简单地输出了这样的内容:重复项 - 9 在索引 0 和 0 找到,这意味着没有找到重复项。我在注释中提到了在索引 8 和 1 找到了 8。我只是不确定如何打印出重复项的位置。我尝试修改了 if 语句,但没成功:if (dataRow[i] != chk[j])if (dataRow[i] == chk[i])。我还尝试将嵌套的 for 循环放入 while 循环中:while (!isValue),但这也没奏效。我想我的教授还想确保数组中的所有值都在 1 到 9 之间,我的想法是这样的:while (!isValue && (dataRow >= 1 && dataRow <= 9)),但我不确定这是否会起作用。我感谢你们能提供的任何帮助。

英文:

For this programming assignment, we are supposed to find the index of a duplicate in this array for one row of a sudoku puzzle. I have this method, static boolean equals(int[] a, int[] a2):

boolean isValue = true;
int[] dataRow = { 9, 8, 7,  6, 5, 4,  3, 2, 8 }; 
int[]     chk = { 9, 8, 7,  6, 5, 4,  3, 1, 8 };
isValue = Arrays.equals(dataRow, chk);
int i, j;

for (i = 0; i &lt; dataRow.length; i++) 
     {
     for (j = 0; j &lt; dataRow.length; j++) 
        {
        if (dataRow[i] == dataRow[j]) 
           {
           System.out.println(&quot;Duplicate - &quot; + dataRow[i] + &quot; found at index &quot; + i + &quot; and &quot; + j);
           //--8 found at index 8 and 1

This program right here simply prints out this: Duplicate - 9 found at index 0 and 0 which means that no duplicate is found. I commented that 8 is found at index 8 and 1. I'm just not sure how to print out where the duplicate was found. I tried modifying the if statement, but that didn't work: if (dataRow[i] != chk[j]), if (dataRow[i] == chk[i]). I also tried putting the nested for loop into a while loop: while (!isValue), but that didn't work either. I think my professor also wants to make sure all the values in the array are between 1-9, and my idea is something like this: while (!isValue &amp;&amp; (dataRow &gt;= 1 &amp;&amp; dataRow &lt;= 9)), but I'm not sure if that will work. I appreciate any help you guys can give me.

答案1

得分: 1

由于输入数组中的值范围有限 [1..9],您可以创建一个小的 check 数组来计算每个数字在输入数组中出现的次数,从而检测重复和缺失的值:

public static void checkForDuplicateAndMissing(int... arr) {
    System.out.println("输入: " + Arrays.toString(arr));
    int[] check = new int[10]; // 全部初始化为 0

    boolean noDuplicates = true;  
    for (int i = 0; i < arr.length; i++) {
        int d = arr[i];
        if (check[d] != 0) {
            System.out.printf("重复的值 %d 在索引 %d 处找到%n", d, i);
            noDuplicates = false;
        }
        check[d]++;
    }
    if (noDuplicates) {
        System.out.println("输入数组中没有重复的值");
    }
  
    boolean allFound = true;
    for (int i = 1; i < check.length; i++) {  // 跳过 0,因为它不在范围 [1..9] 内
        if (check[i] == 0) {
            System.out.println("缺失的值: " + i);
            allFound = false;
        }
    }
    if (allFound) {
        System.out.println("输入数组中包含所有的数字");
    }
    System.out.println("-------\n");
}

测试:

checkForDuplicateAndMissing(9, 8, 7, 6, 5, 4, 3, 2, 8);
checkForDuplicateAndMissing(9, 8, 7, 6, 5, 4, 1, 3, 2);
checkForDuplicateAndMissing(9, 8, 7, 6, 5, 1, 2);

输出:

输入: [9, 8, 7, 6, 5, 4, 3, 2, 8]
重复的值 8 在索引 8 处找到
缺失的值: 1
-------

输入: [9, 8, 7, 6, 5, 4, 1, 3, 2]
输入数组中没有重复的值
输入数组中包含所有的数字
-------

输入: [9, 8, 7, 6, 5, 1, 2]
输入数组中没有重复的值
缺失的值: 3
缺失的值: 4
-------

更新
数组 check 可以存储输入数组中数字的索引(加1),然后可以打印有关第一个索引的信息:

//...
    for (int i = 0; i < arr.length; i++) {
        int d = arr[i];
        if (check[d] != 0) {
            System.out.printf("重复的值 %d 在索引 %d 处找到,第一个值在索引 %d%n", d, i, check[d] - 1);
            noDuplicates = false;
        }
        check[d] = i + 1; // 存储数字的索引,而不是它的计数
    }
英文:

As you have a limited range of values [1..9] in the input array, you could create a small check array to count how many times a digit occurs in the input array, thus detecting duplicates and missing values:

public static void checkForDuplicateAndMissing(int... arr) {
    System.out.println(&quot;Input: &quot; + Arrays.toString(arr));
    int[] check = new int[10]; // populated with 0

    boolean noDuplicates = true;  
    for (int i = 0; i &lt; arr.length; i++) {
        int d = arr[i];
        if (check[d] != 0) {
            System.out.printf(&quot;Duplicate value %d found at index %d%n&quot;, d, i);
            noDuplicates = false;
        }
        check[d]++;
    }
    if (noDuplicates) {
        System.out.println(&quot;No duplicates found in the input array&quot;);
    }
  
    boolean allFound = true;
    for (int i = 1; i &lt; check.length; i++) {  // skipping 0 as it&#39;s not in range [1..9]
        if (check[i] == 0) {
            System.out.println(&quot;Missing value: &quot; + i);
            allFound = false;
        }
    }
    if (allFound) {
        System.out.println(&quot;All digits present in the input array&quot;);
    }
    System.out.println(&quot;-------\n&quot;);
}

Test:

checkForDuplicateAndMissing(9, 8, 7,  6, 5, 4,  3, 2, 8);
checkForDuplicateAndMissing(9, 8, 7,  6, 5, 4,  1, 3, 2);
checkForDuplicateAndMissing(9, 8, 7,  6, 5, 1,  2);

Output:

Input: [9, 8, 7, 6, 5, 4, 3, 2, 8]
Duplicate value 8 found at index 8
Missing value: 1
-------

Input: [9, 8, 7, 6, 5, 4, 1, 3, 2]
No duplicates found in the input array
All digits present in the input array
-------

Input: [9, 8, 7, 6, 5, 1, 2]
No duplicates found in the input array
Missing value: 3
Missing value: 4
-------

Update<br/>
Array check may store indexes of digits at the input array (shifted by 1), then the information about first index may be printed:

//...
    for (int i = 0; i &lt; arr.length; i++) {
        int d = arr[i];
        if (check[d] != 0) {
            System.out.printf(&quot;Duplicate value %d found at index %d, first value is at %d%n&quot;, d, i, check[d] - 1);
            noDuplicates = false;
        }
        check[d] = i + 1; // storing index of the digit instead of its count
    }

output for the first input array:

Input: [9, 8, 7, 6, 5, 4, 3, 2, 8]
Duplicate value 8 found at index 8, first value is at 1
Missing value: 1

huangapple
  • 本文由 发表于 2020年10月15日 03:12:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64360061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定