简体   繁体   English

如何计算重复的数字并写多少次

[英]How to count repeated numbers and write how many times

In an array i want to count all duplicates and to write how many times they are repeated, how should i do it using only loops 在数组中,我想计算所有重复项并写出重复的次数,我应该如何仅使用循环

array example : int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87}; 数组示例:int [] array = {12,23,-22,0,43,545,-4,-55,43,12,0,-999,-87};

This is my code 这是我的代码

public static void main(String[] args) {
        int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
        int cntPoz = 0, cntNeg = 0;

        //for petlja broji pozitivne i negativne clanove array-a
        for (int a = 0; a < array.length; a++) {
            if (array[a] > 0) {
                cntPoz++;
            } else if (array[a] < 0) {
                cntNeg++;
            }
        }

        // kreiranje novih nizova 
        int[] pArr = new int[cntPoz];
        int[] nArr = new int[cntNeg];
        //for petlja iz array-a premjesta sve pozitivne brojeve
        cntPoz = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > 0) {
                pArr[cntPoz++] = array[i];
            }
        }

        //for petlja iz array-a premjesta sve negativne brojeve
        cntNeg = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] < 0) {
                nArr[cntNeg++] = array[i];
            }
        }

        //sortiranje i ispis nizova
        Arrays.sort(pArr);
        Arrays.sort(nArr);
        System.out.println("Originalni niz je : \n" + java.util.Arrays.toString(array) + "\n");
        System.out.println("Niz pozitivnih brojeva : \n" + java.util.Arrays.toString(pArr) + "\n");
        System.out.println("Niz negativnih brojeva : \n" + java.util.Arrays.toString(nArr));
        System.out.println();
        System.out.println();


        // for petlja prebrojava sve duplikate i ispisuje ih
        int dupCnt = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {

                if ((array[j]) == array[i]) {
                    dupCnt++;
                    System.out.println("Duplicates  " + array[j]);
                }
            }
        }
        System.out.println("Number of duplicates  : " + dupCnt);

Try this code. 试试这个代码。 It sorts array of integers first in incrementing order. 它首先以递增顺序对整数数组进行排序。 Then counts the distance between the same numbers and puts them into map with key-number and value-occuarence. 然后计算相同数字之间的距离,并通过键数字和值优先将其放入地图。 Printing format is not that great but is done just for check 打印格式不是很好,但仅用于检查

import java.util.*;
 public class Duplicates {
  public static void main(String[] args)
    {
    Integer[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87, 12};
    mergeSort(array);
    for(Integer x : array)
    {
        System.out.println(x);
    }
    Map<Integer,Integer> duplicates = new HashMap<Integer,Integer>();
    int count = 1;
    for(int i = 0;i < array.length-1;i++)
    {
        if(array[i].equals(array[i+1]))
        {
            count++;
        }
        else
        {
            duplicates.put(array[i],count);
            count=1;
        }
    }
    for ( Integer key : duplicates.keySet() ) 
    {
       System.out.println("Number: " + key + " occuars " + duplicates.get(key) + " times" );
    }        
    }
    public static void mergeSort(Integer[] a)
    {
       if(a.length>1)
       {
       int i,mid = a.length/2;
       Integer[] half1 = new Integer[mid];
       Integer[] half2 = new Integer[a.length-mid];
       for(i=0; i<mid; i++)
       half1[i]=a[i];
       for(; i<a.length; i++)
       half2[i-mid]=a[i];
       mergeSort(half1);
       mergeSort(half2);
       int j=0, k=0;
       for(i=0; j<half1.length&&k<half2.length; i++)
       if(half1[j].compareTo(half2[k])<0)
       {
         a[i]=half1[j];
         j++;
       }
       else
       {
         a[i]=half2[k];
         k++;
       }
       for(; j<half1.length; i++, j++)
       a[i]=half1[j];
       for(; k<half2.length; i++, k++)
       a[i]=half2[k];
 }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM