【问题标题】:How to Find Index of a Duplicate in An Array (Java)如何在数组中查找重复项的索引(Java)
【发布时间】:2021-01-29 06:11:59
【问题描述】:

对于这个编程任务,我们应该在这个数组中为数独游戏的一行找到重复项的索引。我有这个方法,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 < dataRow.length; i++) 
     {
     for (j = 0; j < dataRow.length; j++) 
        {
        if (dataRow[i] == dataRow[j]) 
           {
           System.out.println("Duplicate - " + dataRow[i] + " found at index " + i + " and " + j);
           //--8 found at index 8 and 1

这个程序在这里简单地打印出这个:Duplicate - 9 found at index 0 and 0,这意味着没有找到重复项。我评论说在索引 8 和 1 中找到了 8。我只是不确定如何打印出找到重复项的位置。我尝试修改 if 语句,但没有奏效:if (dataRow[i] != chk[j])if (dataRow[i] == chk[i])。我还尝试将嵌套的 for 循环放入 while 循环:while (!isValue),但这也不起作用。我想我的教授也想确保数组中的所有值都在 1-9 之间,我的想法是这样的:while (!isValue &amp;&amp; (dataRow &gt;= 1 &amp;&amp; dataRow &lt;= 9)),但我不确定这是否可行。感谢你们能给我的任何帮助。

【问题讨论】:

    标签: java arrays for-loop if-statement while-loop


    【解决方案1】:

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

    public static void checkForDuplicateAndMissing(int... arr) {
        System.out.println("Input: " + Arrays.toString(arr));
        int[] check = new int[10]; // populated with 0
    
        boolean noDuplicates = true;  
        for (int i = 0; i < arr.length; i++) {
            int d = arr[i];
            if (check[d] != 0) {
                System.out.printf("Duplicate value %d found at index %d%n", d, i);
                noDuplicates = false;
            }
            check[d]++;
        }
        if (noDuplicates) {
            System.out.println("No duplicates found in the input array");
        }
      
        boolean allFound = true;
        for (int i = 1; i < check.length; i++) {  // skipping 0 as it's not in range [1..9]
            if (check[i] == 0) {
                System.out.println("Missing value: " + i);
                allFound = false;
            }
        }
        if (allFound) {
            System.out.println("All digits present in the input array");
        }
        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);
    

    输出:

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

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

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

    第一个输入数组的输出:

    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
    

    【讨论】:

      猜你喜欢
      • 2021-06-21
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 2011-04-26
      • 1970-01-01
      相关资源
      最近更新 更多