KEMBAR78
Top 10 HashMap Coding Problems | PDF | String (Computer Science) | Computer Programming
0% found this document useful (0 votes)
9 views3 pages

Top 10 HashMap Coding Problems

The document presents the top 10 HashMap coding problems along with their solutions. Each problem includes a brief description and a code snippet demonstrating the solution. The problems range from finding two numbers that sum to a target value to counting word frequencies in a sentence.

Uploaded by

chandra970650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Top 10 HashMap Coding Problems

The document presents the top 10 HashMap coding problems along with their solutions. Each problem includes a brief description and a code snippet demonstrating the solution. The problems range from finding two numbers that sum to a target value to counting word frequencies in a sentence.

Uploaded by

chandra970650
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Top 10 HashMap Coding Problems (with Solutions)

1. Two Sum Problem


Find two numbers in an array that sum to a target value.

Code:
int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
}
return new int[]{-1, -1};
}

2. First Non-Repeating Character


Return the first character that appears only once.

Code:
char firstNonRepeat(String s) {
Map<Character, Integer> map = new LinkedHashMap<>();
for (char c : s.toCharArray()) map.put(c, map.getOrDefault(c, 0) + 1);
for (var entry : map.entrySet()) if (entry.getValue() == 1) return entry.getKey();
return '_';
}

3. Find All Duplicates in an Array


Return elements that appear more than once.

Code:
List<Integer> findDuplicates(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
List<Integer> res = new ArrayList<>();
for (int n : nums) {
map.put(n, map.getOrDefault(n, 0) + 1);
}
for (var e : map.entrySet()) {
if (e.getValue() > 1) res.add(e.getKey());
}
return res;
}

4. Most Frequent Element in Array


Find the most frequent element.

Code:
int mostFrequent(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int n : nums) map.put(n, map.getOrDefault(n, 0) + 1);
int maxKey = nums[0];
for (var e : map.entrySet()) if (e.getValue() > map.get(maxKey)) maxKey = e.getKey();
return maxKey;
}

5. Group Anagrams
Group all anagrams together.

Code:
List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] ch = s.toCharArray();
Arrays.sort(ch);
String key = new String(ch);
map.computeIfAbsent(key, k -> new ArrayList<>()).add(s);
}
return new ArrayList<>(map.values());
}

6. Check Anagram
Check if two strings are anagrams.

Code:
boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;
Map<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) map.put(c, map.getOrDefault(c, 0) + 1);
for (char c : t.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) - 1);
if (map.get(c) < 0) return false;
}
return true;
}

7. Subarray Sum Equals K


Count subarrays with sum = K.

Code:
int subarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1);
int count = 0, sum = 0;
for (int n : nums) {
sum += n;
if (map.containsKey(sum - k)) count += map.get(sum - k);
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
return count;
}

8. Longest Substring Without Repeating Characters


Find length of longest substring without repeating characters.

Code:
int lengthOfLongestSubstring(String s) {
Map<Character, Integer> map = new HashMap<>();
int left = 0, maxLen = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
if (map.containsKey(c) && map.get(c) >= left)
left = map.get(c) + 1;
map.put(c, right);
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}

9. Intersection of Two Arrays


Find intersection of two arrays.

Code:
int[] intersect(int[] nums1, int[] nums2) {
Map<Integer, Integer> map = new HashMap<>();
List<Integer> res = new ArrayList<>();
for (int n : nums1) map.put(n, map.getOrDefault(n, 0) + 1);
for (int n : nums2) {
if (map.getOrDefault(n, 0) > 0) {
res.add(n);
map.put(n, map.get(n) - 1);
}
}
return res.stream().mapToInt(i -> i).toArray();
}

10. Word Frequency Counter


Count frequency of words in a sentence.

Code:
Map<String, Integer> wordFrequency(String text) {
Map<String, Integer> map = new HashMap<>();
String[] words = text.split("\\s+");
for (String w : words) map.put(w, map.getOrDefault(w, 0) + 1);
return map;
}

You might also like