How to get the five least duplicate elements in an array

If I have the following array of strings.

String inStrings[] = {"A", "B", "C", "D", "E", "F", "G", "H", "A", "B", "C", "D", "E", "A", "B", "C", "D", "A", "B"};

And this array is later passed to the method, and I'm not sure how to do it.

    static void getColdSearch(String[] inArray){



}

This method is supposed to take an array, get the lines that will be repeated at least, and then print the five least repeated lines in the output. Duplicate lines should not be next to each other, and if there are less than five lines, they should all be part of the output. For instance. if the arrailist looks like the example above, the result should look something like this.

F //(Occurs once)
G //(Occurs once)
H //(Occurs once)
E //(Occurs twice)
D //(if two different elements repeat the same number of times a random one of them should be printed)

How can i do this?

+4
source share
3 answers

Try java 8 functions

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class GroupingDemo {

    public static void getColdSearch(String[] inArray) {
        Map<String, Long> groupingByLetter = Arrays.stream(inArray)
            .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        List<String> result = groupingByLetter.entrySet().stream()
            .sorted(Map.Entry.comparingByValue())
            .limit(5)
            .map(Map.Entry::getKey)
            .collect(Collectors.toList());

        System.out.println(result);
    }

    public static void main(String[] args) {
        String inStrings[] = {"A", "B", "C", "D", "E", "F", "G", "H", "A", "B", "C", "D", "E", "A", "B", "C", "D", "A", "B"};
        getColdSearch(inStrings);
    }
}
+4

Java 8, . , , , , Java 7 . , , , .

static void getColdSearch(String[] inArray) {
    Map<String, Integer> counterMap = new HashMap<>();


    // load the array in a Map instance
    for (String in : inArray) {
        if (null != counterMap.putIfAbsent(in, 1) ) {
        counterMap.put(in, counterMap.get(in) + 1);
        }
    }

    // Question: why do we need a priority queue?
    // We could also use sort based on the values. Search 
    // stackoverflow.com for "sorting on map value"
    PriorityQueue<String> heap = new PriorityQueue<>(new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
        // sort based on the count.
        return counterMap.get(o1).compareTo(counterMap.get(o2));
        }

    });


    heap.addAll(counterMap.keySet());

    int size = heap.size();
    for (int i = 0; i < size; i++) {
        // you could end it a "5" but I leave that as an exercise.
        String s = heap.poll();
        System.out.println( s + " count: " + counterMap.get(s));
    }
}
+2

Use the maximum heap (priority queue) and the hash map.

Create class

         Class WordCount
          {
             String word;
             int count;
           }  
           PriorityQueue<WordCount>queue // queue to save minimum repeated words
           HashMap<String,Integer>map // save data for each word   

Loop through an array

For the first 5 unique words save in line

check the data on the map. If word exists, refresh counter

check the top item in the queue. if the number of words is higher than the current word, then remove the word from the queue and add this word. Repeat the process until the end of the cycle.

Last poll of all items from the queue

+1
source

Source: https://habr.com/ru/post/1676187/


All Articles