计算整数数组中的重复元素

我有一个整数数组crr_array ,我想计算重复出现的元素。 首先,我读取数组的大小并使用从控制台读取的数字对其进行初始化。 在数组new_array ,我存储了重复的元素。 数组times存储元素的连续出现次数。 然后,我尝试搜索重复序列并以特定格式打印它们。 但是,它不起作用。

 // Get integer array size Scanner input = new Scanner(System.in); System.out.println("Enter array size: "); int size = input.nextInt(); int[] crr_array = new int[size]; int[] new_array= new int[size]; int[] times = new int[size]; // Read integers from the console System.out.println("Enter array elements: "); for (int i = 0; i < crr_array.length; i++) { crr_array[i] = input.nextInt(); times[i] = 1; } // Search for repeated elements for (int j = 0; j < crr_array.length; j++) { for (int i = j; i < crr_array.length; i++) { if (crr_array[j] == crr_array[i] && j != i) { new_array[i] = crr_array[i]; times[i]++; } } } //Printing output for (int i = 0; i < new_array.length; i++) { System.out.println("\t" + crr_array[i] + "\t" + new_array[i] + "\t" + times[i]); } 

我希望输出看起来像这样:

 There are  repeated numbers :  times ... 

例如:

 There are 3 repeated numbers: 22: 2 times 4: 3 times 1: 2 times 

我怎样才能找到重复的元素及其数量? 如何打印如上所示?

这些问题可以通过字典(Java中的HashMap)轻松解决。

  // The solution itself HashMap repetitions = new HashMap(); for (int i = 0; i < crr_array.length; ++i) { int item = crr_array[i]; if (repetitions.containsKey(item)) repetitions.put(item, repetitions.get(item) + 1); else repetitions.put(item, 1); } // Now let's print the repetitions out StringBuilder sb = new StringBuilder(); int overAllCount = 0; for (Map.Entry e : repetitions.entrySet()) { if (e.getValue() > 1) { overAllCount += 1; sb.append("\n"); sb.append(e.getKey()); sb.append(": "); sb.append(e.getValue()); sb.append(" times"); } } if (overAllCount > 0) { sb.insert(0, " repeated numbers:"); sb.insert(0, overAllCount); sb.insert(0, "There are "); } System.out.print(sb.toString()); 

如果您在一组可能的值中有值,则可以使用Counting Sort之类的值

如果不是,你必须在java和Map使用像Dictionary这样的其他数据结构

 int[] array Map 

其中Key =数组值,例如array [i],value = a counter

例:

 int[] array = new int [50]; Map counterMap = new HashMap<>(); //fill the array for(int i=0;i 
 public class DuplicationNoInArray { /** * @param args * the command line arguments */ public static void main(String[] args) throws Exception { int[] arr = { 1, 2, 3, 4, 5, 1, 2, 8 }; int[] result = new int[10]; int counter = 0, count = 0; for (int i = 0; i < arr.length; i++) { boolean isDistinct = false; for (int j = 0; j < i; j++) { if (arr[i] == arr[j]) { isDistinct = true; break; } } if (!isDistinct) { result[counter++] = arr[i]; } } for (int i = 0; i < counter; i++) { count = 0; for (int j = 0; j < arr.length; j++) { if (result[i] == arr[j]) { count++; } } System.out.println(result[i] + " = " + count); } } } 
 for (int i = 0; i < x.length; i++) { for (int j = i + 1; j < x.length; j++) { if (x[i] == x[j]) { y[i] = x[i]; times[i]++; } } } 

您必须使用或阅读关联数组或地图等。 存储重复元素在数组中出现的次数,并为重复的元素本身保存另一个数组,没有多大意义。

你的代码中的问题是在内循环中

  for (int j = i + 1; j < x.length; j++) { if (x[i] == x[j]) { y[i] = x[i]; times[i]++; } } 

与O(n log(n))

 int[] arr1; // your given array int[] arr2 = new int[arr1.length]; Arrays.sort(arr1); for (int i = 0; i < arr1.length; i++) { arr2[i]++; if (i+1 < arr1.length) { if (arr1[i] == arr1[i + 1]) { arr2[i]++; i++; } } } for (int i = 0; i < arr1.length; i++) { if(arr2[i]>0) System.out.println(arr1[i] + ":" + arr2[i]); } 
 package jaa.stu.com.wordgame; /** * Created by AnandG on 3/14/2016. */ public final class NumberMath { public static boolean isContainDistinct(int[] arr) { boolean isDistinct = true; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (arr[i] == arr[j] && i!=j) { isDistinct = false; break; } } } return isDistinct; } public static boolean isContainDistinct(float[] arr) { boolean isDistinct = true; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (arr[i] == arr[j] && i!=j) { isDistinct = false; break; } } } return isDistinct; } public static boolean isContainDistinct(char[] arr) { boolean isDistinct = true; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (arr[i] == arr[j] && i!=j) { isDistinct = false; break; } } } return isDistinct; } public static boolean isContainDistinct(String[] arr) { boolean isDistinct = true; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (arr[i] == arr[j] && i!=j) { isDistinct = false; break; } } } return isDistinct; } public static int[] NumberofRepeat(int[] arr) { int[] repCount= new int[arr.length]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (arr[i] == arr[j] ) { repCount[i]+=1; } } } return repCount; } } call by NumberMath.isContainDistinct(array) for find is it contains repeat or not 

通过int [] repeat = NumberMath.NumberofRepeat(array)调用find find count。 每个位置包含多少重复对应的数组值...

 public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] numbers=new int[5]; String x=null; System.out.print("enter the number 10:"+"/n"); for(int i=0;i<5;i++){ numbers[i] = input.nextInt(); } System.out.print("Numbers : count"+"\n"); int count=1; Arrays.sort(numbers); for(int z=0;z<5;z++){ for(int j=0;j 
 public class ArrayDuplicate { private static Scanner sc; static int totalCount = 0; public static void main(String[] args) { int n, num; sc = new Scanner(System.in); System.out.print("Enter the size of array: "); n =sc.nextInt(); int[] a = new int[n]; for(int i=0;i0){ temp = a[i]; for(int x=0;x 
 package com.core_java; import java.util.Arrays; import java.util.Scanner; public class Sim { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter array size: "); int size = input.nextInt(); int[] array = new int[size]; // Read integers from the console System.out.println("Enter array elements: "); for (int i = 0; i < array.length; i++) { array[i] = input.nextInt(); } Sim s = new Sim(); s.find(array); } public void find(int[] arr) { int count = 1; Arrays.sort(arr); for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] == arr[j]) { count++; } } if (count > 1) { System.out.println(); System.out.println("repeated element in array " + arr[i] + ": " + count + " time(s)"); i = i + count - 1; } count = 1; } } } 
 public class FindRepeatedNumbers { public static void main(String[] args) { int num[]={1,3,2,4,1,2,4,6,7,5}; Arrays.sort(num); for(int j=1;j