【问题标题】:Need to find out the duplicate element in array without using Hashmaps需要在不使用 Hashmaps 的情况下找出数组中的重复元素
【发布时间】:2019-08-13 15:52:22
【问题描述】:

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

此代码将打印出重复的元素。 假设我正在使用元素为 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]+" ");
                }
            }
        }
    }
}

【问题讨论】:

  • 创建一个单独的List&lt;Integer&gt; 以存储找到的任何重复项。也就是说,除了使用List.contains() 方法之外,您还可以确保每个int 只创建一个条目。
  • 使用 TreeMap 而不是 HashMap。 ;) 或者,使用 HashSet 代替,虽然 由 HashMap 支持,但它不是 HashMap。

标签: java arrays loops


【解决方案1】:

以下代码在不创建任何额外数据结构的情况下执行此操作。对于每个元素,它会计算之前遇到的重复项的数量,并且只打印第一个重复项。

如果我在现实世界中这样做,我会使用Set,但我假设你还没有了解它们,所以我只使用你已经创建的数组。

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();
    }
}

【讨论】:

    【解决方案2】:

    一种解决方案是创建一个单独的列表来存储找到的任何重复项。

    也就是说,除了使用 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]
    

    【讨论】:

      【解决方案3】:

      这很简单,但是您需要先对数组进行排序。您只需要知道元素是否存在重复,并在外部 for 循环中进行打印。其余在 cmets 中描述

      Arrays.sort(arr); // Sort Array
      for (int i = 0; i < arr.length; i++) {
          boolean hasDuplicate = false; // Assume that arr[i] is not repeating
          for (int j = i + 1; j < arr.length; j++) {
              // Check if it is repeating
              if (arr[i] == arr[j]) {
                  // If it repeats
                  hasDuplicate = true;
              }
              // Since array is sorted we know that there is no value of arr[i] after this
              if (arr[i] != arr[j]) {
                  // Set i to the last occurrence of arr[i] value
                  i = j - 1;
                  break; // Since there no occurrence of arr[i] value there is no need to continue
              }
          }
          // Print the element at i
          if (hasDuplicate)
              System.out.print(arr[i] + " ");
          // In next iteration loop will start from the index next to the last occurrence of value of arr[i]
      }
      

      【讨论】:

        【解决方案4】:
            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;
                    }
                }
            }
        

        【讨论】:

        • 希望对您有所帮助。您也可以使用 List 和 set 来实现这一点,以减少代码行。编码快乐!! :)
        【解决方案5】:

        如果不使用 Hashmaps,我认为您最好的选择是首先对数组进行排序,然后计算重复项。由于数组现在是有序的,因此您可以在每次切换数字后打印重复项!

        如果这是为了分配,请继续使用谷歌冒泡排序并将其作为方法实现。

        【讨论】:

          猜你喜欢
          • 2015-10-07
          • 2013-10-23
          • 1970-01-01
          • 2014-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-13
          • 1970-01-01
          相关资源
          最近更新 更多