Find the Intersection of Two Arrays
easy array hash table two pointers
🏢google, facebook, amazon
0 views0 likesProblem Description
Given two integer arrays, array1 and array2, write a function in JavaScript that finds the intersection of these two arrays. The intersection of two arrays is a list of unique elements that are present in both arrays. Your function should return the intersection in any order.
Constraints:
- The arrays may contain duplicate elements, but the output must contain only unique elements.
- The function should not use any built-in library methods for set operations.
- Assume the arrays are not necessarily sorted.
- The length of both arrays (array1 and array2) will not exceed 10^4.
Inputs:
- array1: An array of integers. Example: [1, 2, 2, 1]
- array2: An array of integers. Example: [2, 2]
Outputs:
- Return an array containing the unique elements that are present in both array1 and array2. Example: [2]
Examples:
Example 1:
- Input: array1 = [4,9,5], array2 = [9,4,9,8,4]
- Output: [9,4] or [4,9]
Example 2:
- Input: array1 = [1,2,2,1], array2 = [2,2]
- Output: [2]
Example 3:
- Input: array1 = [1,2], array2 = [1,1]
- Output: [1]
Hints
[object Object]
[object Object]
[object Object]