亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要在不使用 Hashmaps 的情況下找出數組中的重復元素

需要在不使用 Hashmaps 的情況下找出數組中的重復元素

qq_遁去的一_1 2023-06-04 15:28:19
我是這里的新手。我想打印出數組中的重復元素。此代碼將打印出重復的元素。假設我正在使用一個大小為 5 的數組,其中的元素[1,2,5,5,5] 此代碼將打?。篋uplicate 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]+" ");                }            }        }    }}
查看完整描述

4 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

下面的代碼沒有創建任何額外的數據結構。對于每個元素,它都會計算之前遇到的重復項的數量,并且只打印第一個重復項。


如果我在現實世界中這樣做,我會使用 aSet但我假設您還沒有了解它們,所以我只使用您已經創建的數組。


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


        System.out.print("Enter " + x + " values: ");

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

            arr[i] = sc.nextInt();

        }


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

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

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

        }

        System.out.println();


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

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

            int numDups = 0;

            for (int j = 0; j < i; j++) {

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

                    numDups++;

                }

            }

            if (numDups == 1) {

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

            }

        }

        System.out.println();

    }

}


查看完整回答
反對 回復 2023-06-04
?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

如果不使用 Hashmap,我認為您最好的選擇是首先對數組進行排序,然后計算重復項。由于數組現在有序,您可以在每次數字切換后打印重復項!

如果這是一項任務,請繼續使用谷歌冒泡排序并將其實現為一種方法。


查看完整回答
反對 回復 2023-06-04
?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

  System.out.println("Duplicate Elements : ");

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

        boolean isDuplicate = false;

        for(int k=0;k<i;k++){

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

                isDuplicate =  true;

                break;

            }

        }

        if(isDuplicate){

            continue;

        }

        int count = 0;

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

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

                count++;

            }

            if(count >1){

                System.out.println(arr[i]);

                break;

            }

        }

    }


查看完整回答
反對 回復 2023-06-04
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

一種解決方案是創建一個單獨的列表來存儲找到的任何重復項。


也就是說,除了使用 List 的 .contains() 方法之外,您還可以確保每個 int 只創建一個條目。


public static void main(String[] args) {


        // Sample array of ints

        int[] ints = {1, 1, 4, 5, 2, 34, 7, 5, 3};


        // Create a separate List to hold duplicated values

        List<Integer> duplicates = new ArrayList<>();


        // Find duplicates

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

            for (int j = 0; j < ints.length; j++) {

                if (ints[i] == ints[j] && // Are the ints the same value?

                        i != j &&  // Ignore if we're looking at the same index 

                        !duplicates.contains(ints[i])) { // Check if our List of duplicates already has this entry

                    duplicates.add(ints[i]); // Add to list of duplicates

                }

            }

        }


        System.out.println("Duplicates: " + duplicates);


    }

輸出:


Duplicates: [1, 5]


查看完整回答
反對 回復 2023-06-04
  • 4 回答
  • 0 關注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號