In the world of data analysis, it's common to compare lists of numbers to find common elements. Imagine you have two lists of integers, and you need to identify which numbers appear in both lists. Your task is to write a function that finds these common elements and ensures that each element in the result appears only once.
Given two integer arrays, nums1
and nums2
, your goal is to return an array containing the intersection of these two arrays. The intersection of two arrays consists of elements that are present in both arrays. Each element in the result should be unique, meaning duplicates are not allowed. The order of elements in the result array can be arbitrary.
Input: nums1 = [1,2,2,1] , nums2 = [2,2]
Output: [2]
Input: nums1 = [4,9,5] , nums2 = [9,4,9,8,4]
Output: [9,4]
The solution should handle large inputs efficiently.
Consider different approaches such as using sets, sorting, or hash maps to optimize performance.
function intersectionUsingHashMap(nums1, nums2) {
const countMap = new Map();
nums1.forEach(num => countMap.set(num, (countMap.get(num) || 0) + 1));
return [...new Set(nums2.filter(num => countMap.has(num)))];
}
const nums11 = [1,2,2,1];
const nums21 = [2,2];
intersectionUsingHashMap(nums11,nums21); //output: [2]
const nums12 = [4,9,5];
const nums22 = [9,4,9,8,4];
intersectionUsingHashMap(nums12,nums22); //output: [9,4]
function intersectionUsingFrequencyArray(nums1, nums2) {
const maxNum = 1000; // Given constraints
const frequency = new Array(maxNum + 1).fill(0);
nums1.forEach(num => frequency[num] = 1);
return [...new Set(nums2.filter(num => frequency[num]))];
}
const nums11 = [1,2,2,1];
const nums21 = [2,2];
intersectionUsingFrequencyArray(nums11,nums21); //output: [2]
const nums12 = [4,9,5];
const nums22 = [9,4,9,8,4];
intersectionUsingFrequencyArray(nums12,nums22); //output: [9,4]
function intersectionUsingIterativeHashSet(nums1, nums2) {
const set1 = new Set(nums1);
const intersection = new Set();
nums2.forEach(num => {
if (set1.has(num)) intersection.add(num);
});
return [...intersection];
}
const nums11 = [1,2,2,1];
const nums21 = [2,2];
intersectionUsingIterativeHashSet(nums11,nums21); //output: [2]
const nums12 = [4,9,5];
const nums22 = [9,4,9,8,4];
intersectionUsingIterativeHashSet(nums12,nums22); //output: [9,4]
function intersectionUsingBinarySearch(nums1, nums2) {
nums1 = [...new Set(nums1)].sort((a, b) => a - b);
const result = [];
function binarySearch(arr, target) {
let low = 0, high = arr.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (arr[mid] === target) return true;
if (arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
return false;
}
nums2.forEach(num => {
if (binarySearch(nums1, num) && !result.includes(num)) {
result.push(num);
}
});
return result;
}
const nums11 = [1,2,2,1];
const nums21 = [2,2];
intersectionUsingBinarySearch(nums11,nums21); //output: [2]
const nums12 = [4,9,5];
const nums22 = [9,4,9,8,4];
intersectionUsingBinarySearch(nums12,nums22); //output: [9,4]
function intersectionTwoSets(nums1, nums2) {
const set1 = new Set(nums1); // Create a set from the first array
const intersectionSet = new Set(); // Set to store the intersection elements
const result = []; // Array to store the final result
// Iterate over the second array and check if the element exists in set1
for (const num of nums2) {
if (set1.has(num)) {
intersectionSet.add(num); // Add to intersectionSet if present
}
}
// Convert the intersectionSet to an array and return
return Array.from(intersectionSet);
}
const nums11 = [1,2,2,1];
const nums21 = [2,2];
intersectionTwoSets(nums11,nums21); //output: [2]
const nums12 = [4,9,5];
const nums22 = [9,4,9,8,4];
intersectionTwoSets(nums12,nums22); //output: [9,4]