[leetCode] 2215. Find the Difference of Two Arrays

2024. 1. 30. 15:14ยท ๊ฐœ๋ฐœ/๐Ÿ’ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
๋ชฉ์ฐจ
  1. ๋ฌธ์ œ
  2. ํ•ด๊ฒฐ๋ฐฉํ–ฅ
  3. ์†”๋ฃจ์…˜ 
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

Given two 0-indexed integer arrays nums1 and nums2, return a list answer of size 2 where:

  • answer[0] is a list of all distinct integers in nums1 which are not present in nums2.
  • answer[1] is a list of all distinct integers in nums2 which are not present in nums1.

Note that the integers in the lists may be returned in any order.

 

Example 1:

Input: nums1 = [1,2,3], nums2 = [2,4,6]
Output: [[1,3],[4,6]]
Explanation:
For nums1, nums1[1] = 2 is present at index 0 of nums2, whereas nums1[0] = 1 and nums1[2] = 3 are not present in nums2. Therefore, answer[0] = [1,3].
For nums2, nums2[0] = 2 is present at index 1 of nums1, whereas nums2[1] = 4 and nums2[2] = 6 are not present in nums2. Therefore, answer[1] = [4,6].

 

 

 

 

ํ•ด๊ฒฐ๋ฐฉํ–ฅ

์ฃผ์–ด์ง„ ๋‘ ๊ฐœ์˜ ๋ฐฐ์—ด์—์„œ ๊ต์ง‘ํ•ฉ์„ ์ฐพ์•„๋‚ด์„œ ๊ฐ๊ฐ์˜ ๋ฐฐ์—ด์—์„œ ๊ต์ง‘ํ•ฉ๋งŒ ์ œ๊ฑฐํ•ด์ฃผ๋ฉด ๋˜์ง€ ์•Š๋‚˜? ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ–ˆ๋‹ค. 

๊ทธ๋ž˜์„œ ์ฒ˜์Œ์— ์ž‘์„ฑํ–ˆ๋˜ ์ž˜ ๋ชป ๋œ ํ•ด๊ฒฐ๋ฒ•์€ ์•„๋ž˜์— ์žˆ๋‹ค. ์ดˆ์•ˆ์€ ์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์•„๋‹˜. ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๋ช‡๊ฐœ๋Š” ํ†ต๊ณผํ•˜๊ณ  ๋ช‡ ๊ฐœ๋Š” ํ†ต๊ณผ ๋ชป ํ•ด์„œ ๋‹ค์‹œ ์ž‘์„ฑํ•˜๋‹ค๊ฐ€ ๋ณ€๊ฒฝ๋œ๊ฒŒ ์•„๋ž˜์™€ ๊ฐ™์Œ. ์ค‘๋ณต๋˜๋Š” ๊ต์ง‘ํ•ฉ์ด ์žˆ์„์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์ƒ๊ฐ ๋ชป ํ•จ. 

 

    let duplicated = []
    
    nums1 = new Set(nums1);
    nums2 = new Set(nums2);
    const n1 = Array.from(nums1)
    const n2 = Array.from(nums2)
    
    for(let i=0; i<n1.length; i++){
        for(let j=0; j<n2.length; j++ ){
            if(n1[i] == n2[j]){
                duplicated.push(n1[i])
            }
        }
    }
    
    const set = new Set(duplicated);
    const dupArr = [...set]
    
    console.log(dupArr)
    const temp1 = n1.filter((v) => dupArr.some((item) => item !== v ? v : ''))
    const temp2 = n2.filter((v) => dupArr.some((item) => item !== v ? v : ''))
                                                       
     console.log(temp1, temp2)
    
    return [temp1, temp2]

 

 

 

 

 

์†”๋ฃจ์…˜ 

var findDifference = function(nums1, nums2) {
    const set1 = new Set(nums1);
    const set2 = new Set(nums2);
    return [
        Array.from(set1).filter(n => !set2.has(n)),
        Array.from(set2).filter(n => !set1.has(n)),
    ]

};

 

Set.prototype.has() ์„ ์‚ฌ์šฉํ•˜๋ฉด Set ๊ฐ์ฒด์— ์ฃผ์–ด์ง„ ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํŒ๋ณ„ํ•ด ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ set1 ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ set2์— ๋Œ€์ž…ํ•ด์„œ ์žˆ๋‚˜ ์—†๋‚˜ ํŒ๋ณ€ํ•ด์„œ ์—†๋Š” ๊ฒƒ๋งŒ filterํ•˜๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค. ์—ญ์‹œ๋‚˜ set2๋„์š”

 

 

 

 

 

 

728x90

'๊ฐœ๋ฐœ > ๐Ÿ’ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[leetCode] 746. Min Cost Climbing Stairs - javascript  (0) 2024.01.31
[leetCode] 1207. Unique Number of Occurrences / javascript  (0) 2024.01.30
[leetCode/EASY] 1431. Kids With the Greatest Number of Candies  (0) 2024.01.17
[leetCode] 34. Find First and Last Position of Element in Sorted Array - Javascript  (0) 2023.11.10
[leetCode/์ฝ”ํ…Œ๊ณต๋ถ€] 2721. Execute Asynchronous Functions in Parallel  (0) 2023.08.22
  1. ๋ฌธ์ œ
  2. ํ•ด๊ฒฐ๋ฐฉํ–ฅ
  3. ์†”๋ฃจ์…˜ 
'๊ฐœ๋ฐœ/๐Ÿ’ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [leetCode] 746. Min Cost Climbing Stairs - javascript
  • [leetCode] 1207. Unique Number of Occurrences / javascript
  • [leetCode/EASY] 1431. Kids With the Greatest Number of Candies
  • [leetCode] 34. Find First and Last Position of Element in Sorted Array - Javascript
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Frontend Developer
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
Ming devlog
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
์ „์ฒด
์˜ค๋Š˜
์–ด์ œ

๊ณต์ง€์‚ฌํ•ญ

  • About Ming ๐Ÿฐ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (261)
    • ๊ฐœ๋ฐœ (255)
      • ๐Ÿ“ TIL (58)
      • โŒ ์˜ค๋ฅ˜ ๋…ธํŠธ (23)
      • React (23)
      • Next.js (11)
      • React-Native (6)
      • JavaScript (10)
      • Flutter (19)
      • Database (1)
      • ๐Ÿ’ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ (21)
      • ๐Ÿ“š English (9)
      • Etc... (25)
      • โ›ต ํ•ญํ•ด99 (49)
    • ๐Ÿ’ป Dev (6)
      • ๐Ÿ“ TIL (4)
      • ๐Ÿ’ฌ Algorithm (1)
      • Etc... (1)

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • react
  • ํ”Œ๋Ÿฌํ„ฐ
  • ๊ฐœ๋ฐœ์žํšŒ๊ณ 
  • nextjs
  • ์ฝ”ํ…Œ๊ณต๋ถ€
  • ๋ฆฌ์•กํŠธ๋„ค์ดํ‹ฐ๋ธŒ
  • ReactNative
  • twid
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
  • todaywhatidid
  • Git
  • ์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
  • Firebase
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • github
  • ํ•ญํ•ด99
  • ๋ฆฌ์•กํŠธ
  • react-native
  • ์˜์–ด๊ณต๋ถ€
  • ์ฝ”๋”ฉ๊ณต๋ถ€
  • leetcode
  • ๊ฐœ๋ฐœ์ž์ผ๊ธฐ
  • flutter
  • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
  • ์ฝ”ํ…Œ
  • ๋ถ€ํŠธ์บ ํ”„
  • ์ด์ฝ”๋…ธ๋ฏธ์ŠคํŠธ
  • ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
  • TIL

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

250x250
hELLO ยท Designed By ์ •์ƒ์šฐ.v4.2.1
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
[leetCode] 2215. Find the Difference of Two Arrays
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๊ฐœ์ธ์ •๋ณด

  • ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ
  • ํฌ๋Ÿผ
  • ๋กœ๊ทธ์ธ

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.