Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- public class NationalLottery {
- public static void main(String[] args) {
- List<Integer> input = List.of(-1, 1, 3, 2, 2, 2, 5, 6, -1, 3, 6);
- List<Integer> duplicates = findDuplicates(input, 2);
- System.out.println(duplicates); // Output: [-1, 3, 6]
- }
- public static List<Integer> findDuplicates(List<Integer> input, int numberOfDuplicates) {
- if (input == null) {
- return new ArrayList<>();
- }
- Map<Integer, Integer> frequencyMap = new HashMap<>();
- for (Integer number : input) {
- if (number != null) {
- frequencyMap.put(number, frequencyMap.getOrDefault(number, 0) + 1);
- }
- }
- return frequencyMap.entrySet().stream()
- .filter(entry -> entry.getValue() == numberOfDuplicates)
- .map(Map.Entry::getKey)
- .collect(Collectors.toList());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement