简体   繁体   English

如何找到一个数字在Java数组中重复多少次

[英]How to find how many times a number is repeated in an array in java

Lets say I have an array of length 10000 with values from 1-9 in it, i need to write a program in java that will find how many number of times a number is repeating in an efficient way. 可以说我有一个长度为10000的数组,其中的值是1-9 ,我需要用Java编写一个程序,该程序将以有效的方式找出一个数字重复多少次。

I thought of defining 9 variables and using if else if ladder i will increase there value if the number is repeating but that wont be a efficient way to do that, kindly suggest me some other approach. 我想定义9变量,如果数字重复则使用i,如果阶梯i会增加那里的值,那我会想到的,但是这样做不是一种有效的方法,请建议我采用其他方法。

use a counter vector. 使用计数器向量。 Lets say we define a vector count[] with 10 positions. 假设我们定义了一个带有10个位置的矢量count []。 In count[1] we store the number of times the '1' appears in our array, in count[2] we count the 2's and so on. 在count [1]中,我们存储“ 1”出现在数组中的次数,在count [2]中,我们计算2的总数,依此类推。 . .

    Map<Integer,Integer> cnts = new HashMap<Integer, Integer>();
    int array[] = new int[1000];

    for(int i=0;i<array.length;i++){
        for (Map.Entry<Integer, Integer> entry : cnts.entrySet()) {
            if(array[i]==entry.getKey()){
                int val = entry.getValue()+1; // increment value by 1
                entry.setValue(val);
            }
        }
    }

Create map Key is number and Value is number of occurrences. 创建地图Key是数字, Value是出现的次数。

Make a HashMap (Key => Number Value => count times) Increase value every time when you find the number. 创建一个HashMap(键=>数字值=>计数次数)每次找到数字时都增加值。

Here some examplecode: 这里是一些示例代码:

if(map.containsKey(currentNumber)){
            int count = map.get(random);
            count++;
            map.put(random, count);
        }

创建新的数组numbers[8]并使用任意循环使用numbers[array[n - 1]]在函数中对数字数量进行计数,其中array是您的数组, n是计数器。

Here is complete running example... hope it helps you 这是完整的运行示例...希望对您有所帮助

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class CountDuplicateItems {

    public static void main(String[] args) {

        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("b");
        list.add("c");
        list.add("a");
        list.add("a");
        list.add("a");

        System.out.println("\nExample 1 - Count 'a' with frequency");
        System.out.println("a : " + Collections.frequency(list, "a"));

        System.out.println("\nExample 2 - Count all with frequency");
        Set<String> uniqueSet = new HashSet<String>(list);
        for (String temp : uniqueSet) {
            System.out.println(temp + ": " + Collections.frequency(list, temp));
        }

        System.out.println("\nExample 3 - Count all with Map");
        Map<String, Integer> map = new HashMap<String, Integer>();

        for (String temp : list) {
            Integer count = map.get(temp);
            map.put(temp, (count == null) ? 1 : count + 1);
        }

        printMap(map);

        System.out.println("\nSorted Map");
        Map<String, Integer> treeMap = new TreeMap<String, Integer>(map);
        printMap(treeMap);

    }

    public static void printMap(Map<String, Integer> map) {

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
        }

    }

}

Output 输出量

Example 1 - Count 'a' with frequency a : 4 示例1-用频率a计数'a':4

Example 2 - Count all with frequency d: 1 b: 2 c: 2 a: 4 示例2-用频率d:1 b:2 c:2 a:4计数全部

Example 3 - Count all with Map Key : d Value : 1 Key : b Value : 2 Key : c Value : 2 Key : a Value : 4 示例3-使用Map键:d值:1键:b值:2键:c值:2键:a值:4全部计数

Sorted Map Key : a Value : 4 Key : b Value : 2 Key : c Value : 2 Key : d Value : 1 排序的映射键:a值:4键:b值:2键:c值:2键:d值:1

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

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