简体   繁体   中英

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.

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.

use a counter vector. Lets say we define a vector count[] with 10 positions. 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. . .

    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.

Make a HashMap (Key => Number Value => count times) Increase value every time when you find the number.

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

Example 2 - Count all with frequency 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

Sorted Map Key : a Value : 4 Key : b Value : 2 Key : c Value : 2 Key : d Value : 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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