Advertisement
adwas33

Untitled

Apr 18th, 2023
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.stream.Collectors;
  6.  
  7. public class NationalLottery {
  8.  
  9. public static void main(String[] args) {
  10. List<Integer> input = List.of(-1, 1, 3, 2, 2, 2, 5, 6, -1, 3, 6);
  11. List<Integer> duplicates = findDuplicates(input, 2);
  12. System.out.println(duplicates); // Output: [-1, 3, 6]
  13. }
  14.  
  15. public static List<Integer> findDuplicates(List<Integer> input, int numberOfDuplicates) {
  16. if (input == null) {
  17. return new ArrayList<>();
  18. }
  19.  
  20. Map<Integer, Integer> frequencyMap = new HashMap<>();
  21.  
  22. for (Integer number : input) {
  23. if (number != null) {
  24. frequencyMap.put(number, frequencyMap.getOrDefault(number, 0) + 1);
  25. }
  26. }
  27.  
  28. return frequencyMap.entrySet().stream()
  29. .filter(entry -> entry.getValue() == numberOfDuplicates)
  30. .map(Map.Entry::getKey)
  31. .collect(Collectors.toList());
  32. }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement