## DEV Community is a community of 729,587 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Alisa Bajramovic

Posted on • Updated on

# Finding the Intersection of Two Arrays

Today's algorithm of the day is a pretty straightforward one: given two arrays, return their intersection. For example, if you were given `nums1 = [3, 4, 5, 1, 3]` and `nums2 = [1, 2, 3, 5]`, you'd want to write a function that returns `[1, 3, 5]`.

I like this problem because there's lots of variations on it--What if the arrays were already sorted? What if you want to optimize memory, but not space? What if one array were significantly longer than another?

## My approach

For this version of the algorithm, I'll use a hash. I find myself often using hashes in my daily algorithms because they don't take up much space or time, and you can be really creative in how you use them.

In this problem, I'll start by taking each number of the first array and putting it into a hash as the key values. If a number is a repeat and has already been seen, and therefore its key already exists in the hash, I'll increment its value by 1.

Then, I'll initialize an empty array, which I'll want to return at the end of the function. I'll go through the second given array, checking each number in it. If that number is a key in the hash, and it has a value greater than 0, I'll put it in the result array, which shows that that number was found in both inputted arrays. I'll also decrement the value in the hash. Finally, I'll return the result.

## The code

The first thing I'll do is initialize a hash that the numbers from num1 will go into. Then, I'll use a for-of loop to walk through the elements of nums1.

``````function intersect(nums1, nums2) {
let num1Hash = {};
for (let num of nums1) {
//...
}
//...
}
``````

If the hash already has the number, then we can just increment its value. If it doesn't, then we can create a new key in the hash as that number, and set its value equal to 1.

``````function intersect(nums1, nums2) {
let num1Hash = {};
for (let num of nums1) {
if (num1Hash[num]) {
num1Hash[num]++;
} else {
num1Hash[num] = 1;
}
}

//...
}
``````

At this point, if the input array were `[3, 4, 5, 1, 3]`, then num1Hash would equal `{'1': 1, '3': 2, '4': 1, '5': 1}`.

Now, we'll want to initialize an array, which is what we'll be returning at the end. We can call this array 'result', and can write the return line at the bottom of the function.

``````function intersect(nums1, nums2) {
let num1Hash = {};
for (let num of nums1) {
if (num1Hash[num]) {
num1Hash[num]++;
} else {
num1Hash[num] = 1;
}
}

let result = [];

//...
return result;
}
``````

Now, we'll want to go through each element in nums2. We want to check if the hash array has that element, and if its value is greater than 0. The reason that a key could have a value equal to 0 is that, inside the if statement, we will be decrementing values if the key has been found.

``````function intersect(nums1, nums2) {
let num1Hash = {};
for (let num of nums1) {
if (num1Hash[num]) {
num1Hash[num]++;
} else {
num1Hash[num] = 1;
}
}

let result = [];

for (let num of nums2) {
if (num1Hash[num] && num1Hash[num] > 0) {
//...
}
}
return result;
}
``````

As I mentioned above, if the num is in the hash, and the value is greater than 0, then the num should be pushed to the result array, and the value in the hash should be decremented.

``````function intersect(nums1, nums2) {
let num1Hash = {};
for (let num of nums1) {
if (num1Hash[num]) {
num1Hash[num]++;
} else {
num1Hash[num] = 1;
}
}

let result = [];

for (let num of nums2) {
if (num1Hash[num] && num1Hash[num] > 0) {
result.push(num);
num1Hash[num]--;
}
}
return result;
}
``````

--

Please let me know in the comments if you have any questions, or alternate solutions to this problem!