File tree 2 files changed +31
-0
lines changed
2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 21
21
263|[ Ugly Number] ( ./0263-ugly-number.js ) |Easy|
22
22
264|[ Ugly Number II] ( ./0264-ugly-number-ii.js ) |Medium|
23
23
345|[ Reverse Vowels of a String] ( ./0345-reverse-vowels-of-a-string.js ) |Easy|
24
+ 350|[ Intersection of Two Arrays II] ( ./0350-intersection-of-two-arrays-ii.js ) |Easy|
24
25
387|[ First Unique Character in a String] ( ./0387-first-unique-character-in-a-string.js ) |Easy|
25
26
451|[ Sort Characters By Frequency] ( ./0451-sort-characters-by-frequency.js ) |Medium|
26
27
459|[ Repeated Substring Pattern] ( ./0459-repeated-substring-pattern.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 350. Intersection of Two Arrays II
3
+ * https://leetcode.com/problems/intersection-of-two-arrays-ii/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given two integer arrays nums1 and nums2, return an array of their intersection.
7
+ *
8
+ * Each element in the result must appear as many times as it shows in both arrays
9
+ * and you may return the result in any order.
10
+ */
11
+
12
+ /**
13
+ * @param {number[] } nums1
14
+ * @param {number[] } nums2
15
+ * @return {number[] }
16
+ */
17
+ var intersect = function ( nums1 , nums2 ) {
18
+ const result = [ ] ;
19
+ const map = { } ;
20
+
21
+ nums1 . forEach ( value => map [ value ] = ( map [ value ] || 0 ) + 1 ) ;
22
+ nums2 . forEach ( value => {
23
+ if ( map [ value ] ) {
24
+ result . push ( value ) ;
25
+ map [ value ] -- ;
26
+ }
27
+ } ) ;
28
+
29
+ return result ;
30
+ } ;
You can’t perform that action at this time.
0 commit comments