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

Java 计算整数数组中的重复元素,java,Java,我有一个整数数组crr\u数组,我想对重复出现的元素进行计数。首先,我读取数组的大小,并用从控制台读取的数字初始化它。在数组新数组中,我存储重复的元素。数组时间存储元素连续出现的次数。然后,我尝试搜索重复序列并以特定格式打印它们。但是,它不起作用 // Get integer array size Scanner input = new Scanner(System.in); System.out.println("Enter array size: "); int size = input.n

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

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

}
如何找到重复元素及其计数?如何按上述方式打印它们?

for(int I=0;Ifor (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]++;
        }

    }

}
对于(int j=i+1;j
带O(n对数(n))

int[]arr1;//您给定的数组
int[]arr2=新int[arr1.length];
Arrays.sort(arr1);
for(int i=0;i0)
System.out.println(arr1[i]+“:”+arr2[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
这类问题可以通过字典(Java中的HashMap)轻松解决

//解决方案本身
HashMap重复=新建HashMap();
对于(int i=0;i1){
总计数+=1;
某人附加(“\n”);
sb.append(如getKey());
某人加上(“:”);
sb.append(如getValue());
某人附加(“时代”);
}
}
如果(总计数>0){
sb.插入(0,“重复编号:);
sb.插入(0,总计数);
sb.插入(0,“有”);
}
系统输出打印(sb.toString());

如果在一组很短的可能值中有值,则可以使用

如果没有,则必须使用另一种数据结构,如java中的字典a
Map

int[] array
Map<Integer, Integer> 
int[]数组
地图
其中Key=数组值,例如数组[i],value=计数器

例如:

int[] array = new int [50];
Map<Integer,Integer> counterMap = new HashMap<>();

//fill the array

    for(int i=0;i<array.length;i++){
         if(counterMap.containsKey(array[i])){
          counterMap.put(array[i], counterMap.get(array[i])+1 );
         }else{
          counterMap.put(array[i], 1);
         }
    }
int[]数组=新的int[50];
Map counterMap=newhashmap();
//填充数组
对于(int i=0;i
public类replicationnoinarray){
/**
*@param args
*命令行参数
*/
公共静态void main(字符串[]args)引发异常{
int[]arr={1,2,3,4,5,1,2,8};
int[]结果=新的int[10];
int计数器=0,计数=0;
对于(int i=0;i
包jaa.stu.com.wordgame;
/**
*由AnandG于2016年3月14日创建。
*/
公开期末班号{
公共静态布尔值isContainDistinct(int[]arr){
布尔值isDistinct=true;
对于(int i=0;i for (int j = i + 1; j < x.length; j++) {

        if (x[i] == x[j]) {
            y[i] = x[i];
            times[i]++;
        }

    }
  // The solution itself 
  HashMap<Integer, Integer> repetitions = new HashMap<Integer, Integer>();

  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<Integer, Integer> 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());
int[] array
Map<Integer, Integer> 
int[] array = new int [50];
Map<Integer,Integer> counterMap = new HashMap<>();

//fill the array

    for(int i=0;i<array.length;i++){
         if(counterMap.containsKey(array[i])){
          counterMap.put(array[i], counterMap.get(array[i])+1 );
         }else{
          counterMap.put(array[i], 1);
         }
    }
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);

        }
    }
}
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
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<num.length;j++)
      {
       if(num[j]==num[j-1])
    {
            System.out.println(num[j]);

       }
   }

       }
     }
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<z;j++){
            if(numbers[z]==numbers[j] & j!=z){
                count=count+1;
            }
        }
        System.out.print(numbers[z]+" - "+count+"\n");
        count=1;

    }
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;i<n;i++){
            System.out.print("Enter the element at position "+i+": ");
            num = sc.nextInt();
            a[enter image description here][1][i]=num;
        }
        System.out.print("Elements in array are: ");
        for(int i=0;i<a.length;i++)
            System.out.print(a[i]+" ");
        System.out.println();
        duplicate(a);
        System.out.println("There are "+totalCount+" repeated numbers:");
    }

    public static void duplicate(int[] a){
        int j = 0,count, recount, temp;
        for(int i=0; i<a.length;i++){
            count = 0;
            recount = 0;
            j=i+1;
            while(j<a.length){
                if(a[i]==a[j])
                    count++;
                j++;
            }
            if(count>0){
                temp = a[i];
                for(int x=0;x<i;x++){
                    if(a[x]==temp)
                        recount++;
                }
                if(recount==0){                 
                    totalCount++;
                    System.out.println(+a[i]+" : "+count+" times");
                }   
            }

        }
    }

}
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 static void duplicatesInteger(int arr[]){
    Arrays.sort(arr);       
    int count=0;
    Set s=new HashSet();
    for(int i=0;i<=arr.length-1;i++){
        for(int j=i+1;j<=arr.length-1;j++){
            if(arr[i]==arr[j] && s.add(arr[i])){
                count=count+1;              
                                }
        }
        System.out.println(count);
    }
}
    int [] numArray = {2,5,3,8,1,2,8,3,3,1,5,7,8,12,134};
    Set<Integer> nums = new HashSet<Integer>();
    
    for (int i =0; i<numArray.length; i++) {
        if(nums.contains(numArray[i]))
            continue;
            int count =1;
            for (int j = i+1; j < numArray.length; j++) {
                if(numArray[i] == numArray[j]) {
                    count++;
                }
                    
            }
            System.out.println("The "+numArray[i]+ " is repeated "+count+" times.");
            nums.add(numArray[i]);
        }
    }