The document contains code snippets for four Java methods that use maps and sets to solve different problems. The first method finds the difference between two integer arrays. The second checks if the occurrences of elements in an integer array are unique. The third checks if two strings contain the same characters with the same frequencies. The fourth counts the number of equal rows and columns in a 2D integer grid.
The document contains code snippets for four Java methods that use maps and sets to solve different problems. The first method finds the difference between two integer arrays. The second checks if the occurrences of elements in an integer array are unique. The third checks if two strings contain the same characters with the same frequencies. The fourth counts the number of equal rows and columns in a 2D integer grid.
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
HashSet<Integer> n1 = new HashSet<Integer>(); HashSet<Integer> n2 = new HashSet<Integer>(); List<List<Integer>> l = new ArrayList<>(); l.add(new ArrayList<>()); l.add(new ArrayList<>()); for(int i = 0; i < nums1.length; i++){ n1.add(nums1[i]); } for(int i = 0; i < nums2.length; i++){ n2.add(nums2[i]); } for(int i = 0; i < nums1.length;i++){ if(n1.contains(nums1[i]) && !n2.contains(nums1[i])) { l.get(0).add(nums1[i]); n1.remove(nums1[i]); } } for(int i = 0; i < nums2.length;i++){ if(n2.contains(nums2[i]) && !n1.contains(nums2[i])) { l.get(1).add(nums2[i]); n2.remove(nums2[i]); } } return l; } } class Solution { public boolean uniqueOccurrences(int[] arr) { Map<Integer, Integer> freq = new HashMap<>(); for (int num : arr) { freq.put(num, freq.getOrDefault(num, 0) + 1); }
Set<Integer> freqSet = new HashSet<>(freq.values());
// If the set size is equal to the map size,
// It implies frequency counts are unique. return freq.size() == freqSet.size(); } } class Solution { public boolean closeStrings(String word1, String word2) { if (word1.length() != word2.length()) { return false; } // Count the occurrences of characters in both strings Map<Character, Integer> count1 = new HashMap<>(); Map<Character, Integer> count2 = new HashMap<>(); for (char c : word1.toCharArray()) { count1.put(c, count1.getOrDefault(c, 0) + 1); } for (char c : word2.toCharArray()) { count2.put(c, count2.getOrDefault(c, 0) + 1); } // Check if the sets of characters are the same if (!count1.keySet().equals(count2.keySet())) { return false; } // Check if the occurrences of characters in both strings are the same Integer[] occurrences1 = count1.values().toArray(new Integer[0]); Integer[] occurrences2 = count2.values().toArray(new Integer[0]); Arrays.sort(occurrences1); Arrays.sort(occurrences2);
return Arrays.equals(occurrences1, occurrences2);
} } class Solution { public int equalPairs(int[][] grid) { int count = 0; int n = grid.length;
for (int c = 0; c < n; c++) { int[] colArray = new int[n]; for (int r = 0; r < n; ++r) { colArray[r] = grid[r][c]; } count += rowCounter.getOrDefault(Arrays.toString(colArray), 0); }