在java中从数组中查找重复元素出现两次以上

我想从数组中找出重复的元素和索引号。我为此写了一个代码。它运行良好,但只有在重复元素的数量超过 2 时才无法生成准确的输出。我从文件中读取值,然后构建一个数组,然后从该数组中搜索重复元素。


import java.io.File;

import java.util.Arrays;

import java.util.Scanner;


public class T1 {

public static void main(String args[]) throws Exception{

    Scanner x=new Scanner(new File("C:\\Duplicate_array.txt"));

    int [] duplicate_data=new int[9];

    int i1=0;

    while(x.hasNext()){

        int a=x.nextInt();

        duplicate_data[i1]=a;

        i1++;

    }

    System.out.println(Arrays.toString(duplicate_data));

    for (int i = 0; i < duplicate_data.length-1; i++) {

        for (int j = i+1; j < duplicate_data.length; j++) {

            if ((duplicate_data[i] == duplicate_data[j]) && (i != j)) {

                System.out.println("Duplicate Element : "+duplicate_data[j]);

                System.out.println("Index of that duplicate element : "+j);

            }

        }

    }

}

}

这是我的输出:


[5, 6, 1, 6, 9, 5, 2, 1, 5]

Duplicate Element : 5

Index of that duplicate element : 5

Duplicate Element : 5

Index of that duplicate element : 8

Duplicate Element : 6

Index of that duplicate element : 3

Duplicate Element : 1

Index of that duplicate element : 7

Duplicate Element : 5

Index of that duplicate element : 8

最后一行错误。它已经在开始的位置找到 5:8。但在程序结束时它再次搜索 5 并给出位置编号。最后的搜索是不必要的。如何摆脱最后一次搜索?



LEATH
浏览 437回答 3
3回答

PIPIONE

您只想遍历数组一次。如果你想要的只是重复,你可以简单地通过跟踪你在使用之前看到的任何值来做到这一点ArrayList:int[] data = {5, 6, 1, 6, 9, 5, 2, 1, 5};System.out.println(Arrays.toString(data));ArrayList<Integer> seenBeforeList = new ArrayList<>();for(int index = 0; index < data.length; index++){&nbsp; &nbsp; int value = data[index];&nbsp; &nbsp; if(seenBeforeList.contains(value)){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Duplicate Element : " + value);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Index of that duplicate element : " + index);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; seenBeforeList.add(value);&nbsp; &nbsp; }}输出:[5, 6, 1, 6, 9, 5, 2, 1, 5]Duplicate Element : 6Index of that duplicate element : 3Duplicate Element : 5Index of that duplicate element : 5Duplicate Element : 1Index of that duplicate element : 7Duplicate Element : 5Index of that duplicate element : 8如果您想按值分组,那么使用 a 更有意义HashMap,将值存储为键,将索引存储为值。然后简单地遍历HashMap.

小唯快跑啊

(i != j)在您的 if 语句中没有必要,因为j总是领先i1,但这不是您的问题。您可以尝试使用重复数组标志来了解何时已经找到重复项。import java.util.Arrays;public class StackOverflow {&nbsp; &nbsp; public static void main(String args[]) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; int[] duplicate_data = {5,6,1,6,9,5,2,1,5};&nbsp; &nbsp; &nbsp; &nbsp; boolean[] duplicate = new boolean[duplicate_data.length];&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(duplicate_data));&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < duplicate_data.length - 1; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = i + 1; j < duplicate_data.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Make sure you haven't flagged this as a duplicate already&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!duplicate[j] && duplicate_data[i] == duplicate_data[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; duplicate[j] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Duplicate Element : " + duplicate_data[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Index of that duplicate element : " + j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}结果:[5, 6, 1, 6, 9, 5, 2, 1, 5]Duplicate Element : 5Index of that duplicate element : 5Duplicate Element : 5Index of that duplicate element : 8Duplicate Element : 6Index of that duplicate element : 3Duplicate Element : 1Index of that duplicate element : 7

叮当猫咪

它正在再次搜索相同的重复项,因为您没有以任何方式存储以前找到的重复项。因此,您必须使用数据结构来存储以前找到的重复项,而不是再次搜索它们。这让我们找到了一个更好的解决方案来查找重复项,它从一开始就使用哈希集,因为它是 O(n) 而不是 O(n^2)import java.io.File;import java.util.Arrays;import java.util.Scanner;public class T1 {&nbsp; &nbsp; public static void main(String args[]) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; Scanner x=new Scanner(new File("C:\\Duplicate_array.txt"));&nbsp; &nbsp; &nbsp; &nbsp; Set<Integer> set = new HashSet<Integer>();&nbsp; &nbsp; &nbsp; &nbsp; int index = 0;&nbsp; &nbsp; &nbsp; &nbsp; while(x.hasNext()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nextNumber = x.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (set.contains(nextNumber)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Duplicate Element : " + nextNumber);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Index of that duplicate element : "+index);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set.add(nextNumber);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}如您所见,使用 时HashSet,我们不需要两个嵌套for循环。我们可以HashSet在常数时间 O(1) 内测试 a 是否包含一个数字,这消除了逐个元素搜索整个数组以找到重复项的需要。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java