Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

ab
ab

Posted on • Edited on

     

Finding the Intersection of Two Arrays

Today'salgorithm of the day is a pretty straightforward one: given two arrays, return their intersection. For example, if you were givennums1 = [3, 4, 5, 1, 3] andnums2 = [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.

functionintersect(nums1,nums2){letnum1Hash={};for(letnumofnums1){//...}//...}
Enter fullscreen modeExit fullscreen mode

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.

functionintersect(nums1,nums2){letnum1Hash={};for(letnumofnums1){if(num1Hash[num]){num1Hash[num]++;}else{num1Hash[num]=1;}}//...}
Enter fullscreen modeExit fullscreen mode

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.

functionintersect(nums1,nums2){letnum1Hash={};for(letnumofnums1){if(num1Hash[num]){num1Hash[num]++;}else{num1Hash[num]=1;}}letresult=[];//...returnresult;}
Enter fullscreen modeExit fullscreen mode

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.

functionintersect(nums1,nums2){letnum1Hash={};for(letnumofnums1){if(num1Hash[num]){num1Hash[num]++;}else{num1Hash[num]=1;}}letresult=[];for(letnumofnums2){if(num1Hash[num]&&num1Hash[num]>0){//...}}returnresult;}
Enter fullscreen modeExit fullscreen mode

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.

functionintersect(nums1,nums2){letnum1Hash={};for(letnumofnums1){if(num1Hash[num]){num1Hash[num]++;}else{num1Hash[num]=1;}}letresult=[];for(letnumofnums2){if(num1Hash[num]&&num1Hash[num]>0){result.push(num);num1Hash[num]--;}}returnresult;}
Enter fullscreen modeExit fullscreen mode

--

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

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
ismailcherri profile image
Ismail Cherri
  • Joined
• Edited on• Edited

I solved it using aSet

functionarrayIntersect(numbersOne,numbersTwo){constinterSection=newSet()for(letnumberofnumbersTwo){if(numbersOne.includes(number)){interSection.add(number)}}return[...interSection]}
Enter fullscreen modeExit fullscreen mode

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromab

DEV Community

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

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp