Java Streams Challenge: Duplicate Elements and Second Highest Number

#50DaysOfStreams – Day 2 & Day 3 Continuing my journey of solving one Java Stream problem daily for 50 days 💪 ✅ Day 2: Find Duplicate Elements in a List 🧩 Problem: Given a list of integers, find all elements that appear more than once. 💡 Solution: List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 2, 3, 6, 7, 3); Set<Integer> duplicates = list.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) .entrySet() .stream() .filter(entry -> entry.getValue() > 1) .map(Map.Entry::getKey) .collect(Collectors.toSet()); System.out.println(duplicates); ✅ Day 3: Find the Second Highest Number in a List 🧩 Problem: Given a list of integers, find the second highest number using Streams. 💡 Solution: List<Integer> list = Arrays.asList(10, 5, 20, 8, 25, 15); Optional<Integer> secondHighest = list.stream() .distinct() .sorted(Comparator.reverseOrder()) .skip(1) .findFirst(); secondHighest.ifPresent(System.out::println); Consistency > Motivation 🔥 See you tomorrow with Day 4! #Java #Streams #FunctionalProgramming #CodingChallenge #BackendDevelopment #100DaysOfCode

To view or add a comment, sign in

Explore content categories