[Algorithm] There are 100,000 pieces of data, print out the number of occurrences of each data/repeated data

1. There are 100,000 pieces of data, print out the number of occurrences of each data

Idea: There is a precondition for this question, that is, the stored data and the number of occurrences cannot be empty. Therefore, we can use the mapcollection class, the key to represent the data, and the value to represent the number of occurrences of the data. When the value is null , indicating that the data has not yet appeared, we can store it in the map. If the value of value is not null, it means that the data has appeared at least once, and we can add 1 to the value of value.
The relevant code is as follows:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class Test2 {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<Integer> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10_0000; i++) {
    
    
            list.add(random.nextInt(10000));
        }
        //        数据    次数
        HashMap<Integer,Integer> map = new HashMap<>();
        for (Integer key : list) {
    
    
            if(map.get(key) == null) {
    
    
                map.put(key,1);
            }else {
    
    
                //说明之前存储过
                int count = map.get(key);
                map.put(key,count+1);
            }
        }
        System.out.println(map);
    }
}

print result:
insert image description here

2. There are 100,000 pieces of data, print out the number of repeated data occurrences

Idea: Compared with the previous question, let us only print the number of repeated data occurrences, and other conditions remain unchanged. It's a little more difficult, but we can use Map.Entry<K, V>this Mapinternal class to store the <key, value>key-value pair mapping relationship to do it, traverse the entry, find the key whose value is greater than 1, and print it out.
The relevant code is as follows:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Test2 {
    
    
    public static void main(String[] args) {
    
    
        ArrayList<Integer> list = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10_0000; i++) {
    
    
            list.add(random.nextInt(10000));
        }
        //        数据    次数
        HashMap<Integer,Integer> map = new HashMap<>();
        for (Integer key : list) {
    
    
            if(map.get(key) == null) {
    
    
                map.put(key,1);
            }else {
    
    
                //说明之前存储过
                int count = map.get(key);
                map.put(key,count+1);
            }
        }
        for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
    
    
            if (entry.getValue() > 1) {
    
    
                System.out.println("重复的数据: " + entry.getKey() + "出现的次数 : " + entry.getValue());
            }
        }
    }
}

print result:
insert image description here

Guess you like

Origin blog.csdn.net/Mubei1314/article/details/122799252