Find the Difference of Two Arrays - Leetcode 2215 - Python

  Рет қаралды 13,977

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 27
@callofduty1
@callofduty1 Жыл бұрын
I also solved it today, and this is my approach: set1 = set(nums1) set2 = set(nums2) return [list(set1 - set2), list(set2 - set1)]
@Andrew_J123
@Andrew_J123 Жыл бұрын
This was my first thought too
@callofduty1
@callofduty1 Жыл бұрын
@@Andrew_J123 The reason I went with this approach is because it's short, simple and easy to understand for beginners,
@DeathSugar
@DeathSugar Жыл бұрын
what numbers it yielded?
@Christian-mn8dh
@Christian-mn8dh 4 ай бұрын
yea why didn't he do this? isn't time complexity the same
@nicktankard1244
@nicktankard1244 2 ай бұрын
​@@Christian-mn8dh yep it's the same time complexity. I guess he wanted to explain how it actually works. the set1 - set2 actually does the exact same loop and lookup under the hood so it's nice to know that.
@jymival2355
@jymival2355 Жыл бұрын
You have explained the problem in a way more complicated way than it has to be. You dont need set to list conversions. When you turn the lists into sets at the beginning to get constant time access to the numbers you also instantly remove any duplicates. From there you can either just use 2 list comprehensions or write a for loop or even use more pythonic code like set1-set2. The problem is obviously aimed at beginners so no need to make it any more confusing by worrying about duplicates in a set and then adding them to another set and then making it a list when there were no duplicates anymore at all in the beginning.
@NeetCodeIO
@NeetCodeIO Жыл бұрын
True that we should iterate the sets rather than lists. But for the pythonic solution, I usually try to avoid those to make it readable for ppl from other langs.
@jymival2355
@jymival2355 Жыл бұрын
@@NeetCodeIOyeah i totally understand why you wouldnt want to use too much pythonic code due to people watching that maybe dont code in python and non pythonic solutions help create a deeper knowledge of coding and the ideas behind it. I love your videos and they help me a ton just wanted to point it out.
@darshsundar544
@darshsundar544 4 ай бұрын
@@jymival2355 bro folded so quickly 😭😭
@basharc
@basharc 4 ай бұрын
two-line solution using list comprehension: nums1, nums2 = set(nums1), set(nums2) return [[n for n in nums1 if n not in nums2], [n for n in nums2 if n not in nums1]]
@TheElementFive
@TheElementFive Жыл бұрын
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: a = set(nums1).difference(set(nums2)) b = set(nums2).difference(set(nums1)) return [list(a), list(b)]
@vignan555
@vignan555 7 ай бұрын
@TheElementFive as the set is unordered how we are getting the relative order
@notgreen11
@notgreen11 Жыл бұрын
my first thoughts for this problem were: 1) O(n+m) space and time, turn the arrays into sets, and use those to form the output 2) sort the arrays with O(n log n) time and O(1) space. Iterate over each array and perform a binary lookup (log n) in the second array. This gives n log m + m log n time
@nikhilaradhya4088
@nikhilaradhya4088 3 ай бұрын
You can reduce some time by iterating through nums1set instead of nums1
@HarshaVardhan-fu9kv
@HarshaVardhan-fu9kv Жыл бұрын
Bro when you converted list into set (line 3) then duplicates are removed, no need to fear about that
@massimomonticelli6010
@massimomonticelli6010 Жыл бұрын
Agreed: instead of traversing nums1 (line 6) and nums2 (line 10), just traverse nums1Set and nums2Set. In this way, both res1 and res2 can be lists.
@NeetCodeIO
@NeetCodeIO Жыл бұрын
Yep, a silly mistake on my part. Should've iterated through the Sets not the Lists.
@blossomprogrammingworld4654
@blossomprogrammingworld4654 Жыл бұрын
good day, I just came across your channel and I have been following it. It's good and I appreciate the explanation. I will appreciate it if more video on programming skill study plan from leetcode was created. Many thanks for considering my request.
@ChandraNandan-mb5sq
@ChandraNandan-mb5sq 8 ай бұрын
class Solution: def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: answer = [[],[]] max_len = max(len(nums1),len(nums2)) for i in range(max_len): if i < len(nums1): if nums1[i] not in nums2 and nums1[i] not in answer[0]: answer[0].append(nums1[i]) if i < len(nums2): if nums2[i] not in nums1 and nums2[i] not in answer[1]: answer[1].append(nums2[i]) return answer
@BRANDONCARRIS
@BRANDONCARRIS Ай бұрын
thanks again papa neet
@asmahamdym
@asmahamdym Жыл бұрын
they say that result is only of length 2 so I initialized res like this: res = [ [ ], [ ] ] and append to res[0] and res[1]
@keerthivasan764
@keerthivasan764 Жыл бұрын
Bro when does google lift hiring freeze
@kirillzlobin7135
@kirillzlobin7135 6 ай бұрын
Great video!
@vignan555
@vignan555 7 ай бұрын
@NeetCodeIO: as the list is unordered how we are getting the relative order
@vignan555
@vignan555 7 ай бұрын
sorry set
@DiaaHaresYusf
@DiaaHaresYusf Жыл бұрын
why don't you use hash map and map each element to it with value 1 , then remove elements from the hash keys while building the answer2 , lastly loop through the remaining keys and build the ans1 and return [ans1 , ans2]
Matrix Diagonal Sum - Leetcode 1572 - Python
8:55
NeetCodeIO
Рет қаралды 9 М.
Asteroid Collision - Stack - Leetcode 735
11:59
NeetCode
Рет қаралды 52 М.
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН
Cheerleader Transformation That Left Everyone Speechless! #shorts
00:27
Fabiosa Best Lifehacks
Рет қаралды 16 МЛН
Каха и дочка
00:28
К-Media
Рет қаралды 3,4 МЛН
7 Most In-Demand Tech Jobs 2025
8:50
The Codebender
Рет қаралды 2,7 М.
1657. Determine if Two Strings Are Close | Arrays | Cases
11:28
Aryan Mittal
Рет қаралды 2,9 М.
String Matching in an Array - Leetcode 1408 - Python
8:01
NeetCodeIO
Рет қаралды 8 М.
UNIQUE NUMBER OF OCCURENCES | LEETCODE 1207 | PYTHON SOLUTION
10:31
Cracking FAANG
Рет қаралды 1,4 М.
Find Pivot Index - Leetcode 724 - Python
8:42
NeetCode
Рет қаралды 59 М.
70% of Programmers can't solve this LeetCode Easy Question
7:32
Python lists, sets, and tuples explained 🍍
15:06
Bro Code
Рет қаралды 343 М.
Try this prank with your friends 😂 @karina-kola
00:18
Andrey Grechka
Рет қаралды 9 МЛН