Coding Challenge Practice - Question 78

Published: (December 14, 2025 at 05:53 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

Problem Description

The task is to find the intersection given two arrays.

Solution Approach

Since the arrays are not sorted and may contain duplicates, a fast lookup method with duplicate removal is ideal. Convert one array into a Set, loop through the other array, and collect matching items in a result Set to ensure each intersecting value appears only once.

Implementation

function getIntersection(arr1, arr2) {
  // Convert the first array to a Set for O(1) lookups
  const set1 = new Set(arr1);
  const result = new Set();

  // Iterate over the second array and add common items to the result set
  for (const item of arr2) {
    if (set1.has(item)) {
      result.add(item);
    }
  }

  // Return the intersection as an array
  return [...result];
}

That’s all folks!

Back to Blog

Related posts

Read more »

Monkey Market

Part 1 Another math gauntlet I get to program a bunch of math operations. Some will be part of several conditionals. I've done it before. I'm confident I can d...

Coding Challenge Practice - Question 69

Problem Description Create a function that returns the n-th term of the “look‑and‑say” sequence as a string. The sequence starts with '1' for n = 1. Each sub...