Technical interviews often feature questions that test your understanding of collections, especially HashMaps. One common challenge involves counting the occurrences of elements within a list. This question helps interviewers assess your ability to handle data aggregation efficiently and avoid pitfalls like NullPointerException.
If you’re new to HashMaps, you may want to check out my Cracking the Basics of HashMap: Key Concepts for Java Developers for foundational knowledge before diving into this post.
In this post, we’ll:
You are given a list of integers, and your task is to return each unique number along with the count of its occurrences in the list. This is a typical problem that tests your understanding of the HashMap data structure and your ability to implement it efficiently.
Here’s an example:
[1, 2, 3, 5, 2, 1]
{1=2, 2=2, 3=1, 5=1}
If the input list is null or empty, the result should return an empty HashMap.
In this solution, we manually check if the HashMap already contains the number as a key. If it does, we increment the value; if it doesn’t, we insert the key with a value of 1.
import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; public class CountNumbers { private HashMapgetCount(List list) { // Initialize HashMap to store number counts HashMap map = new HashMap(); // To avoid NullPointerException in case of a null list if (list != null) { // Iterate through each number in the list for (int num : list) { // If first occurrence, add number with count 1 if (!map.containsKey(num)) { map.put(num, 1); } else { // If the number already exists, increment its count by 1 map.put(num, map.get(num) 1); } } } return map; } public static void main(String[] args) { // Using ArrayList Parameterized Constructor with Collection as argument List numbers = new ArrayList(Arrays.asList(1, 2, 3, 5, 2, 1)); CountNumbers nums = new CountNumbers(); System.out.println(nums.getCount(null)); // Result -> {} System.out.println(nums.getCount(numbers)); // Result -> {1=2, 2=2, 3=1, 5=1} } }
Time Complexity:
Space Complexity: O(n) – In the worst case, all numbers are unique and stored in the HashMap.
The Java HashMap class provides a cleaner and more concise way to handle this problem with the getOrDefault() method. It eliminates the need for if-else logic by returning a default value if the key is not found in the map.
V getOrDefault(Object key, V defaultValue)
Parameters:
Returns: The value to which the specified key is mapped, or defaultValue if the map contains no mapping for the key.
For more information, you can check the official Javadoc for HashMap.
import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; public class CountNumbers { private HashMapgetCount(List list) { // Initialize HashMap to store number counts HashMap map = new HashMap(); // To avoid NullPointerException in case of a null list if (list != null) { // Iterate through each number in the list for (int num : list) { // Cleaner solution using getOrDefault() map.put(num, map.getOrDefault(num, 0) 1); } } return map; } public static void main(String[] args) { // Using ArrayList Parameterized Constructor with Collection as argument List numbers = new ArrayList(Arrays.asList(1, 2, 3, 5, 2, 1)); CountNumbers nums = new CountNumbers(); System.out.println(nums.getCount(null)); // Result -> {} System.out.println(nums.getCount(numbers)); // Result -> {1=2, 2=2, 3=1, 5=1} } }
Aspect | Traditional Approach | Using getOrDefault() |
---|---|---|
Code Readability | Slightly verbose with if-else logic | Cleaner and more concise |
Performance | Same (O(n)) | Same (O(n)) |
Use Case | Works for all Java versions | Requires Java 8 or higher |
Both solutions will yield the same result, but using getOrDefault() makes the code more concise and readable. This question is a common interview favorite because it evaluates your understanding of HashMaps, iteration, and handling null values. It’s also closely related to problems involving frequency counting and data aggregation.
If you found this post helpful, be sure to check out other posts in the Collections Framework Essentials series as well!
Java Fundamentals
Array Interview Essentials
Java Memory Essentials
Happy Coding!
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3