猿问

需要在不使用 Hashmaps 的情况下找出数组中的重复元素

我是这里的新手。我想打印出数组中的重复元素。


此代码将打印出重复的元素。假设我正在使用一个大小为 5 的数组,其中的元素[1,2,5,5,5] 此代码将打印:


Duplicate elements: 5,5,5 //(since 5 is being repeated thrice.)


但我想要这样的输出


Duplicate Elements: 5 //( instead of printing 5 thrice)


import java.util.*;

import java.util.Scanner;


public class duplicateArray{

    public static void main(String args[]){

        Scanner sc=new Scanner(System.in);

        System.out.print("Enter the size of the array: ");

        int x =sc.nextInt();

        int arr[]=new int[x];

        int i,count=0;

            for(i=0;i<x;i++){

                arr[i]=sc.nextInt();

            }

            System.out.print("Array: ");

            for(i=0;i<x;i++){

            System.out.print(arr[i]+" ");

        }

        System.out.println(" ");

        System.out.print("Duplicate elements: ");

        for(i=0;i<arr.length;i++){

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

                if(arr[i]==arr[j]){

                    System.out.print(arr[j]+" ");

                }

            }

        }

    }

}


qq_遁去的一_1
浏览 164回答 4
4回答

隔江千里

下面的代码没有创建任何额外的数据结构。对于每个元素,它都会计算之前遇到的重复项的数量,并且只打印第一个重复项。如果我在现实世界中这样做,我会使用 aSet但我假设您还没有了解它们,所以我只使用您已经创建的数组。import java.util.Scanner;public class DuplicateArray {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter the size of the array: ");&nbsp; &nbsp; &nbsp; &nbsp; int x = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; int[] arr = new int[x];&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter " + x + " values: ");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < x; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[i] = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Array: ");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < x; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(arr[i]+" ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Duplicate elements:");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < arr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int numDups = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < i; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (arr[i] == arr[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numDups++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (numDups == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" " + arr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }}

天涯尽头无女友

如果不使用 Hashmap,我认为您最好的选择是首先对数组进行排序,然后计算重复项。由于数组现在有序,您可以在每次数字切换后打印重复项!如果这是一项任务,请继续使用谷歌冒泡排序并将其实现为一种方法。

30秒到达战场

&nbsp; System.out.println("Duplicate Elements : ");&nbsp; &nbsp; for(int i = 0; i<arr.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; boolean isDuplicate = false;&nbsp; &nbsp; &nbsp; &nbsp; for(int k=0;k<i;k++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(arr[i]== arr[k]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isDuplicate =&nbsp; true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(isDuplicate){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; for(int j=0; j<arr.length; j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(arr[i] == arr[j]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(count >1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(arr[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

12345678_0001

一种解决方案是创建一个单独的列表来存储找到的任何重复项。也就是说,除了使用 List 的 .contains() 方法之外,您还可以确保每个 int 只创建一个条目。public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; // Sample array of ints&nbsp; &nbsp; &nbsp; &nbsp; int[] ints = {1, 1, 4, 5, 2, 34, 7, 5, 3};&nbsp; &nbsp; &nbsp; &nbsp; // Create a separate List to hold duplicated values&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> duplicates = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; // Find duplicates&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < ints.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < ints.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ints[i] == ints[j] && // Are the ints the same value?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i != j &&&nbsp; // Ignore if we're looking at the same index&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; !duplicates.contains(ints[i])) { // Check if our List of duplicates already has this entry&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; duplicates.add(ints[i]); // Add to list of duplicates&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Duplicates: " + duplicates);&nbsp; &nbsp; }输出:Duplicates: [1, 5]
随时随地看视频慕课网APP

相关分类

Java
我要回答