Sitemap

Java program to return the odd numbers and their frequency from an integer array list passed as an input parameter using stream api:

Jul 29, 2024
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class InterviewQuestions {

public static void main(String[] args) {

//I/P: 1, 2, 3, 4, 4, 5, 6, 7, 7, 5
// O/P: {1=1, 3=1, 5=2, 7=2}

List<Integer> intList = Arrays.asList(1, 2, 3, 4, 4, 5, 6, 7, 7, 5);
System.out.println(countOfOddNumber(intList));
}

private static Map<Integer, Long> countOfOddNumber(List<Integer> intList) {

List<Integer> oddList = intList.stream().filter(e1 -> e1 % 2 != 0).collect(Collectors.toList());

return oddList.stream().collect(Collectors.groupingBy((e1 -> e1), Collectors.counting()));

}
}

No responses yet