Hone logo
Hone
Problems

Finding the Intersection of Two Arrays

This challenge focuses on identifying the common elements between two arrays. Finding the intersection of arrays is a fundamental operation with applications in data analysis, database queries, and various other programming scenarios where you need to determine shared elements between datasets. Your task is to write a JavaScript function that efficiently identifies and returns these shared elements.

Problem Description

You are given two arrays, arr1 and arr2, which may contain numbers, strings, or a mix of data types. Your goal is to write a function findIntersection(arr1, arr2) that returns a new array containing only the elements that are present in both arr1 and arr2. The order of elements in the returned array does not matter. Duplicate elements should only appear once in the result, even if they appear multiple times in the input arrays.

Key Requirements:

  • The function must accept two arrays as input.
  • The function must return a new array containing the intersection of the two input arrays.
  • The returned array should not contain any duplicate elements.
  • The function should handle arrays containing different data types (numbers, strings, etc.).
  • The function should handle empty arrays gracefully.

Expected Behavior:

The function should iterate through the arrays and compare elements. If an element exists in both arrays, it should be added to the result array (if it's not already present).

Edge Cases to Consider:

  • Empty input arrays: If either or both arrays are empty, the intersection is an empty array.
  • Arrays with duplicate elements: Ensure that the resulting intersection array contains only unique elements.
  • Arrays with different data types: The function should handle mixed data types correctly.
  • Large arrays: Consider the efficiency of your solution for large input arrays.

Examples

Example 1:

Input: arr1 = [1, 2, 2, 1], arr2 = [2, 2, 3]
Output: [2]
Explanation: The element '2' is present in both arrays.  Duplicates are removed in the output.

Example 2:

Input: arr1 = [4, 9, 5], arr2 = [9, 4, 9, 8, 4]
Output: [4, 9] or [9, 4] (order doesn't matter)
Explanation: Both '4' and '9' are present in both arrays. Duplicates are removed.

Example 3:

Input: arr1 = [1, 2, 3], arr2 = [4, 5, 6]
Output: []
Explanation: There are no common elements between the two arrays.

Example 4:

Input: arr1 = [], arr2 = [1, 2, 3]
Output: []
Explanation: One of the arrays is empty, so the intersection is empty.

Constraints

  • The input arrays arr1 and arr2 can contain any number of elements (0 or more).
  • The elements within the arrays can be of any data type (numbers, strings, booleans, etc.).
  • The maximum length of either array is 1000.
  • The function should have a time complexity of O(n + m), where n and m are the lengths of arr1 and arr2 respectively. While a brute-force O(n*m) solution will work, it is not ideal for larger arrays.

Notes

Consider using a Set data structure to efficiently track the elements that have already been added to the intersection array. This will help avoid duplicates and improve performance. Think about how to handle different data types when comparing elements. The goal is to find elements that are strictly equal (using ===).

Loading editor...
javascript