Problem
- We have been given a list key that has a reference to the data points. This list can be large and it can be challenging to store them as it is.
- Engineers are looking to compress this list and for that, they want to filter out all the keys that are repeated more than 10%.
- Our job is to filter all the keys in that list that have an occurrence of more than 10% in the list.
Input Data
- The input list contains keys that have reference to data points on the disk.
[7, 6, 4, 4, 4, 1, 2, 2, 3, 9, 7, 2, 5, 7, 2, 1, 3, 3, 6, 8, 8, 6, 6, 6, 1, 9, .. ]
Output
- Output list contains all the elements that have occurred more than 10% in the list.
[2, 3, 6, 7, 8, 9]
If we group each key in the list {1=8, 2=13, 3=14, 4=8, 5=8, 6=11, 7=11, 8=11, 9=16}, then we see that above list of keys have occurrence more than 10%.
Before jumping to the solution consider giving an attempt.
Solution
- Sample Input Data
List<Integer> dataPoints = IntStream.rangeClosed(1, 100)
.mapToObj(i -> new Random().nextInt(1, 10))
.toList();
System.out.println(dataPoints.size());
System.out.println(dataPoints);
- At first, we need to group each key with its count. Streams API provides a grouping-by-and-counting function that helps us perform group-by-count operations.
Map<Integer, Long> groupedDataPoints = dataPoints.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(groupedDataPoints);
- Once we have grouped keys we can filter the list with our condition of counting more than 10%
- Once we have filtered the target data points, we return the keys as a List.
List<Integer> targetDataPoints = groupedDataPoints.entrySet()
.stream()
.filter((e) -> (e.getValue()
.floatValue() / dataPoints.size()) >= 0.10)
.map(Map.Entry::getKey)
.toList();
System.out.println(targetDataPoints);
Code
Find all the code for this & other exercises on GitHub
Over to you! Write down your solution in the comment section for others to get back to it
Before You Leave
- Upgrade your Java skills with Grokking the Java Interview.
- If you want to upskill your Java skills, you should definitely check out
[NEW] Master Spring Boot 3 & Spring Framework 6 with Java
[ 38 hrs content, 4.7/5 stars, 6+ students already enrolled]
Other Problems List