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 Жыл бұрын
This was my first thought too
@callofduty1 Жыл бұрын
@@Andrew_J123 The reason I went with this approach is because it's short, simple and easy to understand for beginners,
@DeathSugar Жыл бұрын
what numbers it yielded?
@Christian-mn8dh4 ай бұрын
yea why didn't he do this? isn't time complexity the same
@nicktankard12442 ай бұрын
@@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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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.
@darshsundar5444 ай бұрын
@@jymival2355 bro folded so quickly 😭😭
@basharc4 ай бұрын
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 Жыл бұрын
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)]
@vignan5557 ай бұрын
@TheElementFive as the set is unordered how we are getting the relative order
@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
@nikhilaradhya40883 ай бұрын
You can reduce some time by iterating through nums1set instead of nums1
@HarshaVardhan-fu9kv Жыл бұрын
Bro when you converted list into set (line 3) then duplicates are removed, no need to fear about that
@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 Жыл бұрын
Yep, a silly mistake on my part. Should've iterated through the Sets not the Lists.
@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-mb5sq8 ай бұрын
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Ай бұрын
thanks again papa neet
@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 Жыл бұрын
Bro when does google lift hiring freeze
@kirillzlobin71356 ай бұрын
Great video!
@vignan5557 ай бұрын
@NeetCodeIO: as the list is unordered how we are getting the relative order
@vignan5557 ай бұрын
sorry set
@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]