Created
May 19, 2025 22:24
-
-
Save Keijiro-Ida/8e81347cace4b4f7f8bcddcf6235a07e to your computer and use it in GitHub Desktop.
349. Intersection of Two Arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function intersection(nums1: number[], nums2: number[]): number[] { | |
const nums1Set = new Set(nums1); | |
const nums2Set = new Set(nums2); | |
const result: number[] = []; | |
for(const num of nums1Set) { | |
if(nums2Set.has(num)) { | |
result.push(num); | |
} | |
} | |
return result; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
/** | |
* @param Integer[] $nums1 | |
* @param Integer[] $nums2 | |
* @return Integer[] | |
*/ | |
function intersection($nums1, $nums2) { | |
$nums1_unique = array_unique($nums1); | |
$nums2_map = array_flip(array_unique($nums2)); | |
$result = []; | |
foreach($nums1_unique as $num) { | |
if(isset($nums2_map[$num])) { | |
$result[] = $num; | |
} | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment