post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/top-k-frequent-elements/discuss/1193491/python-hashmap-solution
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: m={} ans=[] m=Counter(nums) s=sorted(m,key=m.get) for i in range(k,0,-1): ans.append(s.pop()) return ans
top-k-frequent-elements
python hashmap solution
mech2tech
1
194
top k frequent elements
347
0.648
Medium
6,100
https://leetcode.com/problems/top-k-frequent-elements/discuss/741192/Python3-A-few-approaches-O(NlogN)orO(NlogK)orO(N)
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq = dict() #frequency table for x in nums: freq[x] = 1 + freq.get(x, 0) return sorted(freq, key=freq.get)[-k:]
top-k-frequent-elements
[Python3] A few approaches O(NlogN)|O(NlogK)|O(N)
ye15
1
93
top k frequent elements
347
0.648
Medium
6,101
https://leetcode.com/problems/top-k-frequent-elements/discuss/741192/Python3-A-few-approaches-O(NlogN)orO(NlogK)orO(N)
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq = dict() #frequency table for x in nums: freq[x] = 1 + freq.get(x, 0) h = [] for x, v in freq.items(): heappush(h, (v, x)) if len(h) > k: heappop(h) return [x for _, x in h]
top-k-frequent-elements
[Python3] A few approaches O(NlogN)|O(NlogK)|O(N)
ye15
1
93
top k frequent elements
347
0.648
Medium
6,102
https://leetcode.com/problems/top-k-frequent-elements/discuss/741192/Python3-A-few-approaches-O(NlogN)orO(NlogK)orO(N)
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq = Counter(nums) #frequency table return nlargest(k, freq, key=freq.get)
top-k-frequent-elements
[Python3] A few approaches O(NlogN)|O(NlogK)|O(N)
ye15
1
93
top k frequent elements
347
0.648
Medium
6,103
https://leetcode.com/problems/top-k-frequent-elements/discuss/741192/Python3-A-few-approaches-O(NlogN)orO(NlogK)orO(N)
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq = Counter(nums) vals = list(freq.values()) def partition(lo, hi): """Return parition of vals[lo:hi].""" i, j = lo+1, hi-1 while i <= j: if vals[i] < vals[lo]: i += 1 elif vals[j] > vals[lo]: j -= 1 else: vals[i], vals[j] = vals[j], vals[i] i += 1 j -= 1 vals[lo], vals[j] = vals[j], vals[lo] return j shuffle(vals) lo, hi = 0, len(vals) while lo < hi: mid = partition(lo, hi) if mid + k < len(vals): lo = mid + 1 elif mid + k == len(vals): break else: hi = mid return [k for k, v in freq.items() if v >= vals[mid]]
top-k-frequent-elements
[Python3] A few approaches O(NlogN)|O(NlogK)|O(N)
ye15
1
93
top k frequent elements
347
0.648
Medium
6,104
https://leetcode.com/problems/top-k-frequent-elements/discuss/741192/Python3-A-few-approaches-O(NlogN)orO(NlogK)orO(N)
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: bucket = [[] for _ in nums] for num, freq in Counter(nums).items(): bucket[-freq].append(num) return list(islice(chain(*bucket), k))
top-k-frequent-elements
[Python3] A few approaches O(NlogN)|O(NlogK)|O(N)
ye15
1
93
top k frequent elements
347
0.648
Medium
6,105
https://leetcode.com/problems/top-k-frequent-elements/discuss/741192/Python3-A-few-approaches-O(NlogN)orO(NlogK)orO(N)
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq = Counter(nums) bucket = [[] for _ in nums] for x, v in freq.items(): bucket[-v].append(x) ans = [] for x in bucket: ans.extend(x) if len(ans) >= k: break return ans
top-k-frequent-elements
[Python3] A few approaches O(NlogN)|O(NlogK)|O(N)
ye15
1
93
top k frequent elements
347
0.648
Medium
6,106
https://leetcode.com/problems/top-k-frequent-elements/discuss/740666/Python-Heap-Simple
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: numCntr = collections.Counter(nums) # 1-liner # return heapq.nlargest(k, numCntr.keys(), key = numCntr.get) hp = [] for ky,val in numCntr.items(): heapq.heappush(hp,(val,ky)) if len(hp) > k: heapq.heappop(hp) return [heapq.heappop(hp)[1] for _ in range(len(hp))]
top-k-frequent-elements
Python Heap Simple
akrishna06
1
212
top k frequent elements
347
0.648
Medium
6,107
https://leetcode.com/problems/top-k-frequent-elements/discuss/2848584/one-line-python3-solution
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: return [i[0] for i in Counter(nums).most_common(k)]
top-k-frequent-elements
one line python3 solution
Cosmodude
0
1
top k frequent elements
347
0.648
Medium
6,108
https://leetcode.com/problems/top-k-frequent-elements/discuss/2842346/Python-3
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: sort_nums_dict = dict(sorted(Counter(nums).items(), key=lambda item: item[1], reverse=True)) most_freq = [] for key in sort_nums_dict.keys(): most_freq.append(key) if len(most_freq) == k: break return most_freq
top-k-frequent-elements
Python 3
Geniuss87
0
1
top k frequent elements
347
0.648
Medium
6,109
https://leetcode.com/problems/top-k-frequent-elements/discuss/2840572/Python-solution-or-98-faster
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: counter = Counter(nums) heap = [] ans = [] for key in counter: heapq.heappush(heap, (-counter[key], key)) for i in range(k): ans.append(heapq.heappop(heap)[1]) return ans
top-k-frequent-elements
Python solution | 98% faster
maomao1010
0
5
top k frequent elements
347
0.648
Medium
6,110
https://leetcode.com/problems/top-k-frequent-elements/discuss/2840427/Top-K-frequents-python-O(n)
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: if len(nums) == k: return nums counter = collections.Counter(nums) # create a hashmap of counter frecTable = [None]*len(nums) for num, frec in counter.items(): if frecTable[frec-1]: frecTable[frec-1] += [num] else: frecTable[frec-1] = [num] result = [] for _nums in reversed(frecTable): if len(result) < k and _nums: result.extend(_nums) elif len(result) == k: break return result
top-k-frequent-elements
Top K frequents - python - O(n)
DavidCastillo
0
2
top k frequent elements
347
0.648
Medium
6,111
https://leetcode.com/problems/top-k-frequent-elements/discuss/2837076/Python3-easy-to-understand
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: d = Counter(nums) res = [] for K, V in sorted(d.items(), key = lambda x: -x[1]): res.append(K) k -= 1 if k == 0: return res
top-k-frequent-elements
Python3 - easy to understand
mediocre-coder
0
3
top k frequent elements
347
0.648
Medium
6,112
https://leetcode.com/problems/top-k-frequent-elements/discuss/2835401/Python3-Bucket-Sort
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: # Create bucket to collect number for each frequency # Length of nums is the maximum of possisble frequency # The frequency is the "index" of bucket # For example: 5 same numbers, 100, in nums. [100, 100, 100, 100, 100] # len(nums) is 5, bucket[5] = [100] refers to frequency 5 has number 100. bucket = [[] for _ in range(len(nums)+1)] freq = {} # You can use collections.Counter for num in nums: freq[num] = freq.get(num, 0) + 1 # (key, val) = (num, freq) # Put number in associated "frequency" bucket for key, val in freq.items(): bucket[val].append(key) res = [] pos = len(bucket) - 1 while len(res) < k and pos >= 0: res.extend(bucket[pos]) # Use extend since bucket[pos] is a list pos -= 1 # Go backwards, i.e. start from the largest frequency return res
top-k-frequent-elements
Python3 Bucket Sort
geom1try
0
3
top k frequent elements
347
0.648
Medium
6,113
https://leetcode.com/problems/top-k-frequent-elements/discuss/2834749/3-Solutions-Python-explained
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: count = Counter(nums) count = {k: v for k, v in sorted(count.items(), key=lambda item: item[1],reverse=True)} return list(count.keys())[:k]
top-k-frequent-elements
3 Solutions Python explained
parthjain9925
0
4
top k frequent elements
347
0.648
Medium
6,114
https://leetcode.com/problems/top-k-frequent-elements/discuss/2834749/3-Solutions-Python-explained
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: count = Counter(nums) maxheap = [[-freq,val] for val,freq in count.items()] heapq.heapify(maxheap) ans=[] for i in range(k): _,num = heapq.heappop(maxheap) ans.append(num) return ans
top-k-frequent-elements
3 Solutions Python explained
parthjain9925
0
4
top k frequent elements
347
0.648
Medium
6,115
https://leetcode.com/problems/top-k-frequent-elements/discuss/2834749/3-Solutions-Python-explained
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: count = Counter(nums) c = [[] for i in range(len(nums)+1)] for key,val in count.items(): c[val].append(key) ans=[] for i in range(len(c)-1,0,-1): for j in c[i]: ans.append(j) if len(ans)==k: return ans
top-k-frequent-elements
3 Solutions Python explained
parthjain9925
0
4
top k frequent elements
347
0.648
Medium
6,116
https://leetcode.com/problems/top-k-frequent-elements/discuss/2832971/Python-or-O(2n)-time-complexity-or-Improvisation-on-neetcode's-solution
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: frequency = [set()] count = {} for el in nums: frequency.append(set()) prev_cnt = count.get(el, 0) cnt = prev_cnt + 1 frequency[prev_cnt].discard(el) frequency[cnt].add(el) count[el] = cnt ord_vals = [] for item in frequency[::-1]: for el in item: ord_vals.append(el) if len(ord_vals) == k: return ord_vals
top-k-frequent-elements
Python | O(2n) time complexity | Improvisation on neetcode's solution
gowt7
0
2
top k frequent elements
347
0.648
Medium
6,117
https://leetcode.com/problems/top-k-frequent-elements/discuss/2830934/Easy-to-understand-priority-queue-full-implementation-without-shortcuts
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: # create a map of value: key pairs which later will be added to the priority queue # O(n) space counter = collections.Counter(nums) objs = [(v, k) for k, v in counter.items()] # initialize heap heap = [] # added k elements to the queue in O(k) time and O(k) space for _ in range(k): heapq.heappush(heap, objs.pop()) # added N - k elements to the queue in O(nlog(k)) time for _ in range(len(objs)): heapq.heappushpop(heap, objs.pop()) # extract keys in O(k) time ans = [k for v, k in heap] return ans
top-k-frequent-elements
Easy to understand - priority queue - full implementation without shortcuts
user9015KF
0
2
top k frequent elements
347
0.648
Medium
6,118
https://leetcode.com/problems/top-k-frequent-elements/discuss/2828273/O(N)-Complexity
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: d={} f=[[] for i in range(len(nums) + 1)] for num in nums: d[num]=1+d.get(num,0) for n,c in d.items(): f[c].append(n) res = [] for i in range(len(f) - 1, 0, -1): print(f[i]) for n in f[i]: print(res) res.append(n) if len(res) == k: return res
top-k-frequent-elements
O(N) Complexity
nermeen_wageh
0
2
top k frequent elements
347
0.648
Medium
6,119
https://leetcode.com/problems/top-k-frequent-elements/discuss/2813532/Python-Hashmap-Easy-Solution
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: a = Counter(nums) a = dict(sorted(a.items(), key = lambda x:x[1], reverse = True)) r = [] for i, value in a.items(): r.append(i) return r[:k]
top-k-frequent-elements
Python Hashmap Easy Solution
bishlajatin
0
8
top k frequent elements
347
0.648
Medium
6,120
https://leetcode.com/problems/top-k-frequent-elements/discuss/2812301/Beats-99.48-Documented-Python-Solution
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: # Items turns each dict pair into tuples ex. (1, 3) # Using the lambda func x: x[1] sorts based on the second item in the tuple ie. the count # Reverse sorts in descending order res = dict(sorted(Counter(nums).items(), key=lambda x:x[1], reverse=True)).keys() return list(res)[:k]
top-k-frequent-elements
Beats 99.48% Documented Python Solution
Danryanh7
0
1
top k frequent elements
347
0.648
Medium
6,121
https://leetcode.com/problems/top-k-frequent-elements/discuss/2806950/Python-BruteForce-or-Dict-or-Faster-than-70
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: c_d = {} for num in nums: if num in c_d: c_d[num] += 1 else: c_d[num] = 1 d = dict(sorted(c_d.items(), key=lambda item: item[1], reverse = True)) return list(d.keys())[:k]
top-k-frequent-elements
Python BruteForce | Dict | Faster than 70%
prameshbajra
0
12
top k frequent elements
347
0.648
Medium
6,122
https://leetcode.com/problems/top-k-frequent-elements/discuss/2801562/Easy-python3-solution-using-dictionary-and-sorting
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: myDict = dict() for num in nums: if not num in myDict: myDict[num] = 1 else: myDict[num] += 1 myDict = sorted(myDict.items(), key=lambda item: item[1]) myDict = myDict[::-1] result = [] for i in range(k): result.append(myDict[i][0]) return result
top-k-frequent-elements
Easy python3 solution, using dictionary and sorting
gorge_t88
0
6
top k frequent elements
347
0.648
Medium
6,123
https://leetcode.com/problems/top-k-frequent-elements/discuss/2801173/Python-2-line-solution
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: nums_counter = Counter(nums) return sorted(nums_counter, key=lambda x:-nums_counter[x])[:k]
top-k-frequent-elements
Python 2 line solution
remenraj
0
4
top k frequent elements
347
0.648
Medium
6,124
https://leetcode.com/problems/top-k-frequent-elements/discuss/2800640/Easy-peasy-3-line-solution
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: t = Counter(nums) m = t.most_common(k) return([x for(x,v) in m])
top-k-frequent-elements
Easy peasy 3 line solution
_debanjan_10
0
4
top k frequent elements
347
0.648
Medium
6,125
https://leetcode.com/problems/top-k-frequent-elements/discuss/2778569/Python%3A-Heap-sort-%2B-Hashmap-oror-Time-Complexity-O(nlogn)
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: d={} for i in nums: if i in d.keys(): d[i]+=1 else: d[i]=1 ans=[] m=[] for i,j in d.items(): heapq.heappush(m,[-j,i]) i=0 while i<k: a=heapq.heappop(m) ans.append(a[1]) i+=1 return ans
top-k-frequent-elements
Python: Heap sort + Hashmap || Time Complexity= O(nlogn)
utsa_gupta
0
6
top k frequent elements
347
0.648
Medium
6,126
https://leetcode.com/problems/top-k-frequent-elements/discuss/2776590/sol-using-hashmap
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: hashMap = {} for i in nums: if (i in hashMap): hashMap[i] +=1 else: hashMap[i] = 1 print(hashMap) s = sorted(hashMap.items(), key= lambda x:x[1], reverse = True)[:k] return [i[0] for i in s ]
top-k-frequent-elements
sol using hashmap
bharatdikshit
0
1
top k frequent elements
347
0.648
Medium
6,127
https://leetcode.com/problems/top-k-frequent-elements/discuss/2756378/Easy-Python-solution-using-dictionary
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: dic={} for i in nums: if i in dic: dic[i]+=1 else: dic[i]=1 ret = [] for i in range(k): t = max(dic,key=dic.get) ret.append(t) dic.pop(t,None) return ret
top-k-frequent-elements
Easy Python solution using dictionary
parthdixit
0
17
top k frequent elements
347
0.648
Medium
6,128
https://leetcode.com/problems/top-k-frequent-elements/discuss/2747294/Efficient-Python-Dictionary-Sets-Memory-Usage-Beats-98.8
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: length = len(nums) # total number of values in the array frequency_dict = dict() # dictionary with keys of numbers and values of how often they showed up for position in range(length): if nums[position] not in frequency_dict: # if the number has not been seen yet frequency_dict[nums[position]] = 1 # now it has been seen once else: # number has already been seen and is in frequency_dict frequency_dict[nums[position]] += 1 # not it has been seen one more time list_of_frequencies = sorted(frequency_dict.values()) # sort the frequencies (ascending) most_common_first = list_of_frequencies[::-1] # reverse this list of frequencies (descending) # now there is a list, named most_common_first, that starts with the most common # frequencies at which numbers occurred in the array nums most_common_values = set() # set of the k most common values in nums for frequency in range(k): # this iterates through k times, 0,1,...,k-1 # now we want to know the k most frequent elements, which can be returned in any order (use a set) for integer, how_often in frequency_dict.items(): # iterate through dictionary # in this dictionary, integers are keys and how_often is how often they occurred (value) # most_common_first stores the frequencies - starting with the frequencies of numbers occurring most often if how_often == most_common_first[frequency]: # if it occurred at one of the k most common frequencies most_common_values.add(integer) if k == len(most_common_values): # error checking, useful for troubleshooting return most_common_values ```
top-k-frequent-elements
Efficient Python Dictionary Sets - Memory Usage Beats 98.8%
vixeyfox
0
10
top k frequent elements
347
0.648
Medium
6,129
https://leetcode.com/problems/top-k-frequent-elements/discuss/2729811/Python-Solution-using-QuickSelect-O(n)-average-time-O(n2)-worst-case
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: nTof = {} for i in nums: if nTof.get(i) is None: nTof[i] = 1 else: nTof[i] += 1 unique = list(nTof.keys()) def partition(unique, l, r): pivot, p = nTof[unique[r]], r i = l while i < p: if nTof[unique[i]] > pivot: unique[i], unique[p-1] = unique[p-1], unique[i] unique[p], unique[p-1] = unique[p-1], unique[p] i -= 1 p -= 1 i += 1 return p def quickSelect(unique, l, r, k): p = partition(unique, l, r) if p > k: quickSelect(unique, l, p-1, k) if p < k: quickSelect(unique, p+1, r, k) return unique[k:] return quickSelect(unique, 0, len(unique)-1, len(unique)-k)
top-k-frequent-elements
Python Solution using QuickSelect -- O(n) average time, O(n^2) worst case
gabrnavarro
0
4
top k frequent elements
347
0.648
Medium
6,130
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2270388/PYTHON-3-SIMPLE-or-EASY-TO-UNDERSTAND
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: a = [] for i in nums1: if i not in a and i in nums2: a.append(i) return a
intersection-of-two-arrays
[PYTHON 3] SIMPLE | EASY TO UNDERSTAND
omkarxpatel
5
135
intersection of two arrays
349
0.704
Easy
6,131
https://leetcode.com/problems/intersection-of-two-arrays/discuss/961733/Python-Simple-Solution
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1) &amp; set(nums2))
intersection-of-two-arrays
Python Simple Solution
lokeshsenthilkumar
5
666
intersection of two arrays
349
0.704
Easy
6,132
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2677813/Python-Solution-oror-Hashtable
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: d = {} if len(nums1) > len(nums2): nums1,nums2=nums2,nums1 for i in nums1: d[i] = 0 for i in nums2: if i not in d: continue else: d[i] = d[i]+1 res = [] for k,v in d.items(): if v > 0: res.append(k) return res
intersection-of-two-arrays
Python Solution || Hashtable
Graviel77
2
522
intersection of two arrays
349
0.704
Easy
6,133
https://leetcode.com/problems/intersection-of-two-arrays/discuss/1721717/Python-Simplest-Solution-with-explanation-oror-Beg-to-Adv-oror-Two-pointer
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: res = [] # list 1, for saving same elem of num1 and num2. fres = [] # list 2, for converting set to list. for i in range(len(nums1)): # read elem from nums1 for j in range(len(nums2)): # read elem from nums2 if nums1[i] == nums2[j]: # identical elements of both list. res.append(nums1[i]) # appending identical elements in list 1. fres = set(res) # using set to remove duplicate elements &amp; typecasting it back to list. return fres
intersection-of-two-arrays
Python Simplest Solution with explanation || Beg to Adv || Two pointer
rlakshay14
2
147
intersection of two arrays
349
0.704
Easy
6,134
https://leetcode.com/problems/intersection-of-two-arrays/discuss/1378750/Sets-intersection-98-speed
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1) &amp; set(nums2))
intersection-of-two-arrays
Sets intersection, 98% speed
EvgenySH
2
295
intersection of two arrays
349
0.704
Easy
6,135
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2770326/Intuitive-Python-Solution-Hashmap
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: hashmap = {} res = set() for i in range(len(nums1)): hashmap[nums1[i]] = i for n in nums2: if n in hashmap: res.add(n) return res Time and Space complexity: O(m + n) = O(max(m, n))
intersection-of-two-arrays
Intuitive Python Solution, Hashmap
tragob
1
107
intersection of two arrays
349
0.704
Easy
6,136
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2644107/Python-O(N)
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: ans=[] n=len(nums1) for i in nums1: if i in nums2: ans.append(i) return list(set(ans))
intersection-of-two-arrays
Python [O(N)]
Sneh713
1
202
intersection of two arrays
349
0.704
Easy
6,137
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2354614/Solution
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: nums1,nums2 = set(nums1), set(nums2) return (nums1.intersection(nums2))
intersection-of-two-arrays
Solution
fiqbal997
1
27
intersection of two arrays
349
0.704
Easy
6,138
https://leetcode.com/problems/intersection-of-two-arrays/discuss/1975746/Python-One-liner-Set-Intersection-Operation
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1)&amp;set(nums2))
intersection-of-two-arrays
[Python] One-liner Set Intersection Operation
zayne-siew
1
60
intersection of two arrays
349
0.704
Easy
6,139
https://leetcode.com/problems/intersection-of-two-arrays/discuss/1855402/Python-Simple-and-Elegant!-Multiple-Solutions!
class Solution(object): def intersection(self, nums1, nums2): ans, d = [], {} for n in nums1: if n not in d: d[n] = 1 for n in nums2: if n in d and d[n] == 1: d[n] = 2 for k in d: if d[k] == 2: ans.append(k) return ans
intersection-of-two-arrays
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
1
103
intersection of two arrays
349
0.704
Easy
6,140
https://leetcode.com/problems/intersection-of-two-arrays/discuss/1855402/Python-Simple-and-Elegant!-Multiple-Solutions!
class Solution(object): def intersection(self, nums1, nums2): ans = [] for n in set(nums1): if n in set(nums2): ans.append(n) return ans
intersection-of-two-arrays
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
1
103
intersection of two arrays
349
0.704
Easy
6,141
https://leetcode.com/problems/intersection-of-two-arrays/discuss/1855402/Python-Simple-and-Elegant!-Multiple-Solutions!
class Solution(object): def intersection(self, nums1, nums2): return [n for n in set(nums1) if n in set(nums2)]
intersection-of-two-arrays
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
1
103
intersection of two arrays
349
0.704
Easy
6,142
https://leetcode.com/problems/intersection-of-two-arrays/discuss/1855402/Python-Simple-and-Elegant!-Multiple-Solutions!
class Solution(object): def intersection(self, nums1, nums2): return set(nums1) &amp; set(nums2)
intersection-of-two-arrays
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
1
103
intersection of two arrays
349
0.704
Easy
6,143
https://leetcode.com/problems/intersection-of-two-arrays/discuss/1855402/Python-Simple-and-Elegant!-Multiple-Solutions!
class Solution(object): def intersection(self, nums1, nums2): return set(nums1).intersection(set(nums2))
intersection-of-two-arrays
Python - Simple and Elegant! Multiple Solutions!
domthedeveloper
1
103
intersection of two arrays
349
0.704
Easy
6,144
https://leetcode.com/problems/intersection-of-two-arrays/discuss/1356955/Easy-python-solution
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: i=0 s=set() while i<len(nums1): if nums1[i] in nums2: s.add(nums1[i]) i+=1 out=list(s) return out
intersection-of-two-arrays
Easy python solution
Qyum
1
55
intersection of two arrays
349
0.704
Easy
6,145
https://leetcode.com/problems/intersection-of-two-arrays/discuss/1087462/One-line-solution%3A-Python3-(98.19-faster)
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return (set(nums1) &amp; set(nums2))
intersection-of-two-arrays
One line solution: Python3 (98.19% faster)
iamvatsalpatel
1
124
intersection of two arrays
349
0.704
Easy
6,146
https://leetcode.com/problems/intersection-of-two-arrays/discuss/400034/Python-2-pointers
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: nums1.sort() nums2.sort() pointer1, pointer2, out = 0, 0, set() while pointer1 < len(nums1) and pointer2 < len(nums2): if nums1[pointer1] == nums2[pointer2]: out.add(nums1[pointer1]) pointer1 += 1 pointer2 += 1 elif nums1[pointer1] > nums2[pointer2]: pointer2 += 1 else: pointer1 += 1 return list(out)
intersection-of-two-arrays
Python 2 pointers
aj_to_rescue
1
101
intersection of two arrays
349
0.704
Easy
6,147
https://leetcode.com/problems/intersection-of-two-arrays/discuss/343333/Solution-in-Python-3-(beats-~100)-(one-line)
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return set(nums1) &amp; set(nums2) - Junaid Mansuri
intersection-of-two-arrays
Solution in Python 3 (beats ~100%) (one line)
junaidmansuri
1
405
intersection of two arrays
349
0.704
Easy
6,148
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2846096/python3
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: d = {} # store frequency of nums in nums1 # store freq for n in nums1: if n not in d: d[n] = True ans = [] # answer # see if nums in nums2 are also in nums1 for n in nums2: # if in nums1 and not already added if (n in d) and d[n]: # add and set to false (so no duplicates) ans.append(n) d[n] = False return ans
intersection-of-two-arrays
python3
wduf
0
2
intersection of two arrays
349
0.704
Easy
6,149
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2846096/python3
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: # return intersection between unique nums from nums1 and nums2 return list(set(nums1) &amp; set(nums2))
intersection-of-two-arrays
python3
wduf
0
2
intersection of two arrays
349
0.704
Easy
6,150
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2839985/Pythonor-Easiest-Solution-in-4-linesor
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: x=set(nums1) y=set(nums2) z=x.intersection(y) return list(z)
intersection-of-two-arrays
Python| Easiest Solution in 4 lines|
Vasu0514
0
3
intersection of two arrays
349
0.704
Easy
6,151
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2838825/python-one-liner-solution
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1)&amp;set(nums2))
intersection-of-two-arrays
python one-liner solution
sahityasetu1996
0
3
intersection of two arrays
349
0.704
Easy
6,152
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2836886/Python-Easy-Solution-Beats-89.93-Using-Sets
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: # Make sets of the values in nums1 and nums2 set1 = set(nums1) set2 = set(nums2) # Get the intersection of them intersection = set1 &amp; set2 # Return the answer as an array (list) answer = list(intersection) return answer
intersection-of-two-arrays
Python Easy Solution Beats 89.93% Using Sets
vixeyfox
0
2
intersection of two arrays
349
0.704
Easy
6,153
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2830878/Using-for-loop
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: a =[] for i in nums1: if i in nums2 and i not in a: a.append(i) return a
intersection-of-two-arrays
๊Using for loop
Es-ppx
0
5
intersection of two arrays
349
0.704
Easy
6,154
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2826368/Fast-and-Easy-Python-Solution
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: lists = [nums1, nums2] smallest = min(lists, key=len) lists.remove(smallest) checked = set() values = [] for num in smallest: if num not in checked: if num in lists[0]: values += [num] checked.add(num) return values
intersection-of-two-arrays
Fast and Easy Python Solution
PranavBhatt
0
4
intersection of two arrays
349
0.704
Easy
6,155
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2825055/Python-one-liner
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1).intersection(set(nums2)))
intersection-of-two-arrays
Python one liner
WhyYouCryingMama
0
4
intersection of two arrays
349
0.704
Easy
6,156
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2817178/python-dictionary(hash-map)-O(n)-beats-80
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: result = [] hashmap = {} for i in set(nums1): hashmap[i]=1 for i in set(nums2): if hashmap.get(i) is not None: result.append(i) return result
intersection-of-two-arrays
python dictionary(hash map) O(n) beats 80%
sudharsan1000m
0
3
intersection of two arrays
349
0.704
Easy
6,157
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2817119/2-liner-code-in-python
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: uniqa_list = [] for x in nums2: if x in nums1 and x not in uniqa_list: uniqa_list.append(x) return uniqa_list
intersection-of-two-arrays
2 liner code in python
user8539if
0
1
intersection of two arrays
349
0.704
Easy
6,158
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2809036/Easy-One-line-code
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return set(nums1) &amp; set(nums2)
intersection-of-two-arrays
Easy One line code
umeshsakinala
0
2
intersection of two arrays
349
0.704
Easy
6,159
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2790320/One-Line-Python-Solution
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: # return set cast of the larger list intersection with smaller list return set(nums1).intersection(nums2) if (len(nums1) >= len(nums2)) else set(nums2).intersection(nums1)
intersection-of-two-arrays
One Line Python Solution
laichbr
0
4
intersection of two arrays
349
0.704
Easy
6,160
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2787861/Python-oror-Beats-78.56
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: nums = [] for i in nums1: if i in nums2: if i not in nums: nums.append(i) return (nums)
intersection-of-two-arrays
Python || Beats 78.56%
fatin_istiaq
0
3
intersection of two arrays
349
0.704
Easy
6,161
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2785084/Python-Easy-Python-solution
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: if(len(nums1)>len(nums2)): mx = nums1 mn = nums2 else: mx = nums2 mn = nums1 return list(set(mn).intersection(mx))
intersection-of-two-arrays
[Python] Easy Python solution
thesaderror
0
3
intersection of two arrays
349
0.704
Easy
6,162
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2780070/Python-solution-with-dictionary
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: ans = dict() #you can use a set() answer = set() for i in nums1: if i in ans: ans[i] += 1 else: ans[i] = 0 for j in nums2: if j in ans: answer.add(j) else: continue return answer
intersection-of-two-arrays
Python solution with dictionary
Siddhi_Dhonsale
0
2
intersection of two arrays
349
0.704
Easy
6,163
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2779014/One-liner-Python-Solution
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1).intersection(set(nums2)))
intersection-of-two-arrays
One liner Python Solution
gpersonnat
0
1
intersection of two arrays
349
0.704
Easy
6,164
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2778292/python-easy-solution
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: nums2 =list(set(nums2)); nums1 = list(set(nums1)); res = nums1 + nums2 ; z , y=[],[]; for x in res: if x not in z : z.append(x); else: y.append(x); return y
intersection-of-two-arrays
python easy solution
seifsoliman
0
1
intersection of two arrays
349
0.704
Easy
6,165
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2758941/Python-Easy-Solution
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: res=[] for num in nums1: if num in nums2: res.append(num) return set(res)
intersection-of-two-arrays
Python Easy Solution
Aayushman19
0
2
intersection of two arrays
349
0.704
Easy
6,166
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2746387/Python3-solution-short-concise-one-line
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(i for i in nums1 if i in set(i for i in nums2)))
intersection-of-two-arrays
Python3 solution; short, concise, one line
FaridehGhani
0
4
intersection of two arrays
349
0.704
Easy
6,167
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2682097/python-easy
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1).intersection(set(nums2)))
intersection-of-two-arrays
python easy
changvijay54
0
4
intersection of two arrays
349
0.704
Easy
6,168
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2657348/Python3-(O(n%2Bm))-Faster-than-92-and-less-memory-usage-less-90
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: hmap={} ans=[] m=len(nums2) n=len(nums1) if n>m: for i in range(m): hmap[nums2[i]]=1 for x in nums1: if x in hmap and hmap[x]!=0: hmap[x]=0 ans.append(x) else: for j in range(n): hmap[nums1[j]]=1 for y in nums2: if y in hmap and hmap[y]!=0: hmap[y]=0 ans.append(y) return ans
intersection-of-two-arrays
Python3 (O(n+m))- Faster than 92% and less memory usage < 90%
Mufeed-Amir
0
29
intersection of two arrays
349
0.704
Easy
6,169
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2627482/Simple-Dictionary-solution-73.44-faster-TC-O(N)
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: dict_num = {} for i in nums1: if not i in dict_num: dict_num[i] = 1 else: dict_num[i] += 1 ans = [] for i in nums2: if i in dict_num: ans.append(i) del dict_num[i] return ans
intersection-of-two-arrays
Simple Dictionary solution ,73.44 % faster, TC O(N)
nik_praj
0
15
intersection of two arrays
349
0.704
Easy
6,170
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2598289/Faster-than-91.30
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: dic1 = set() dic2 = set() result = [] for i in range(len(nums1)): dic1.add(nums1[i]) for i in range(len(nums2)): dic2.add(nums2[i]) for val in dic1: if val in dic2: result.append(val) return result
intersection-of-two-arrays
Faster than 91.30%
happinessss57
0
20
intersection of two arrays
349
0.704
Easy
6,171
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2593625/Python3-Solution-with-using-single-hashset
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: s = set(nums1) res = [] for num in nums2: if num in s: res.append(num) s.remove(num) return res
intersection-of-two-arrays
[Python3] Solution with using single hashset
maosipov11
0
27
intersection of two arrays
349
0.704
Easy
6,172
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2568983/python-solution-95.25-faster-using-sets-and-intersection-function-only-one-line-code!
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1).intersection(set(nums2)))
intersection-of-two-arrays
python solution 95.25% faster using sets and intersection function, only one line code!
samanehghafouri
0
28
intersection of two arrays
349
0.704
Easy
6,173
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2561818/Python-3-most-easy-one-liner-solution-(86-faster)
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return set(nums1).intersection(set(nums2))
intersection-of-two-arrays
Python 3 most easy one liner solution (86% faster)
khushie45
0
80
intersection of two arrays
349
0.704
Easy
6,174
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2546650/Simplest-Python-solution-possible
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return set(nums1) &amp; set(nums2)
intersection-of-two-arrays
Simplest Python solution possible
betaal
0
31
intersection of two arrays
349
0.704
Easy
6,175
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2530784/Python-Fast-and-Simple-(-1-Liner-)
class Solution: def intersection(self, n1: List[int], n2: List[int]) -> List[int]: return set(n1).intersection(set(n2))
intersection-of-two-arrays
Python Fast and Simple ( 1 Liner )
SouravSingh49
0
60
intersection of two arrays
349
0.704
Easy
6,176
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2524573/Python-one-liner
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return [i for i in set(nums1) if i in nums2]
intersection-of-two-arrays
Python one-liner
Mark_computer
0
18
intersection of two arrays
349
0.704
Easy
6,177
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2524329/Simple-Python-defaultdict-solution
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: result = [] d = defaultdict(bool) # default is False for n in nums1: d[n] = True for n in nums2: if d[n]: result.append(n) d[n] = False return result
intersection-of-two-arrays
Simple Python defaultdict solution
manualmsdos
0
14
intersection of two arrays
349
0.704
Easy
6,178
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2484781/Python-(Simple-Solution-and-Beginner-Friendly)
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: arr = nums2 if len(nums1) > len(nums2) else nums1 arr2 = nums1 if len(nums1) > len(nums2) else nums2 output = set() for i in arr: if i in arr2 and i not in output: output.add(i) return output
intersection-of-two-arrays
Python (Simple Solution and Beginner-Friendly)
vishvavariya
0
41
intersection of two arrays
349
0.704
Easy
6,179
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2424110/Simple-Approach-or-Beginners-or-one-line-or-Fast
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return set(nums1).intersection(set(nums2))
intersection-of-two-arrays
✔ Simple Approach | Beginners | one-line | Fast
ayushigupta2409
0
59
intersection of two arrays
349
0.704
Easy
6,180
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2382217/Python-Two-pointers
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: result = set() for i in range(len(nums1)): for j in range(len(nums2)): if nums1[i] == nums2[j]: result.add(nums2[j]) return result
intersection-of-two-arrays
Python [Two pointers]
Yauhenish
0
26
intersection of two arrays
349
0.704
Easy
6,181
https://leetcode.com/problems/intersection-of-two-arrays/discuss/2351335/Python-One-Liner-Faster-Solution-using-set().intersection()
class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: return list(set(nums1).intersection(set(nums2)))
intersection-of-two-arrays
[Python] One Liner Faster Solution using set().intersection()
Buntynara
0
10
intersection of two arrays
349
0.704
Easy
6,182
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/1231807/easy-or-two-pointer-method-or-python
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: nums1.sort() nums2.sort() one=0 two=0 ans=[] while one < len(nums1) and two < len(nums2): if nums1[one] < nums2[two]: one+=1 elif nums2[two] < nums1[one]: two+=1 else: ans.append(nums1[one]) one+=1 two+=1 return ans
intersection-of-two-arrays-ii
easy | two pointer method | python
chikushen99
23
1,300
intersection of two arrays ii
350
0.556
Easy
6,183
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/1785178/Python-Simple-Python-Solution-By-Counting-Element-in-Both-List
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: result = [] for i in nums1: if i in nums2 and i not in result: result = result + [i]*min(nums1.count(i),nums2.count(i)) return result
intersection-of-two-arrays-ii
[ Python ] ✔✔ Simple Python Solution By Counting Element in Both List 🔥✌
ASHOK_KUMAR_MEGHVANSHI
11
508
intersection of two arrays ii
350
0.556
Easy
6,184
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/1495437/Python-or-Explained-or-Easy-to-Understand
class Solution: def intersect(self, nums1: list[int], nums2: list[int]) -> list[int]: counter1, counter2 = Counter(nums1), Counter(nums2) counter = counter1 &amp; counter2 return list(counter.elements())
intersection-of-two-arrays-ii
✅ Python | Explained | Easy to Understand
IamUday
7
627
intersection of two arrays ii
350
0.556
Easy
6,185
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/2036264/Python-Beats-98-Multiple-Solutions-One-linerTwo-liners-Simple-Clean-use-Dictionary
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: dict1, dict2 = Counter(nums1), Counter(nums2) res = [] for key in dict1: if key in dict1 and key in dict2: res += [key] * min(dict1[key], dict2[key]) return res
intersection-of-two-arrays-ii
[Python] Beats 98% Multiple Solutions One-liner/Two-liners Simple Clean use Dictionary
ziaiz-zythoniz
4
166
intersection of two arrays ii
350
0.556
Easy
6,186
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/2036264/Python-Beats-98-Multiple-Solutions-One-linerTwo-liners-Simple-Clean-use-Dictionary
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: d1, d2 = Counter(nums1), Counter(nums2) return [num for num in d1 &amp; d2 for count in range(min(d1[num], d2[num]))]
intersection-of-two-arrays-ii
[Python] Beats 98% Multiple Solutions One-liner/Two-liners Simple Clean use Dictionary
ziaiz-zythoniz
4
166
intersection of two arrays ii
350
0.556
Easy
6,187
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/2036264/Python-Beats-98-Multiple-Solutions-One-linerTwo-liners-Simple-Clean-use-Dictionary
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: return [num for num in Counter(nums1) &amp; Counter(nums2) for count in range(min(Counter(nums2)[num], Counter(nums2)[num]))]
intersection-of-two-arrays-ii
[Python] Beats 98% Multiple Solutions One-liner/Two-liners Simple Clean use Dictionary
ziaiz-zythoniz
4
166
intersection of two arrays ii
350
0.556
Easy
6,188
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/2036264/Python-Beats-98-Multiple-Solutions-One-linerTwo-liners-Simple-Clean-use-Dictionary
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: dict1, dict2 = Counter(nums1), Counter(nums2) return list((dict1 &amp; dict2).elements())
intersection-of-two-arrays-ii
[Python] Beats 98% Multiple Solutions One-liner/Two-liners Simple Clean use Dictionary
ziaiz-zythoniz
4
166
intersection of two arrays ii
350
0.556
Easy
6,189
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/1975754/Python-One-liner-Counter-Intersection-Operation
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: return list((Counter(nums1)&amp;Counter(nums2)).elements())
intersection-of-two-arrays-ii
[Python] One-liner Counter Intersection Operation
zayne-siew
4
163
intersection of two arrays ii
350
0.556
Easy
6,190
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/1579388/Python-sol-O(n%2Bm)-using-one-dictionary
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: res = [] dic = {} for i in nums1: if i in dic: dic[i] += 1 else: dic[i] = 1 for j in nums2: if j in dic and dic[j] != 0: res.append(j) dic[j] -= 1 return res
intersection-of-two-arrays-ii
Python sol O(n+m) , using one dictionary
elayan
4
305
intersection of two arrays ii
350
0.556
Easy
6,191
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/2595333/SIMPLE-PYTHON3-SOLUTION-faster-using-collections
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: c1 = collections.Counter(nums1) c2 = collections.Counter(nums2) res = [] for key in c1: if key in c2: res+= [key]*(min(c1[key],c2[key])) return res
intersection-of-two-arrays-ii
✅✔ SIMPLE PYTHON3 SOLUTION ✅✔faster using collections
rajukommula
3
221
intersection of two arrays ii
350
0.556
Easy
6,192
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/2505772/Easy-Python3-code
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: ans=[] for i in nums1: if i in nums2: ans.append(i) nums2.remove(i) return ans
intersection-of-two-arrays-ii
Easy Python3 code
khushie45
3
165
intersection of two arrays ii
350
0.556
Easy
6,193
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/1249869/Pythonic-Solution-O(n2)
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: output=[] for i in set(nums1): x=min(nums1.count(i),nums2.count(i)) if x: output=output+[i]*x return output
intersection-of-two-arrays-ii
Pythonic Solution O(n^2)
_jorjis
3
316
intersection of two arrays ii
350
0.556
Easy
6,194
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/1149292/Python-3-Solution-using-hashtables-beats-94-in-runtime-(sometimes-lol)
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: d1 = dict() d2 = dict() intersection = list() # Count the occurrences of each element in nums1 and nums2. for i in nums1: d1[i] = d1.get(i,0) + 1 for i in nums2: d2[i] = d2.get(i,0) + 1 smaller_dict = None # Determine which dictionary has less unique elements. if len(d1) == len(d2) or len(d1) < len(d2): smaller_dict = d1 else: smaller_dict = d2 # Iterate through the smaller dictionary and if the element exists in # in both dictionaries determine which has less copies # and add that many copies to array being returned. for key, val in smaller_dict.items(): if key in d1 and key in d2: copies = min(d1[key], d2[key]) while copies != 0: intersection.append(key) copies -= 1 return intersection
intersection-of-two-arrays-ii
Python 3 Solution using hashtables beats 94% in runtime (sometimes lol)
CaffeineEnthusiast
2
228
intersection of two arrays ii
350
0.556
Easy
6,195
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/304855/Python3-Beat-100-concise-version
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: from collections import Counter if not nums1 or not nums2: return [] a=Counter(nums1) b=Counter(nums2) res=a&amp;b return list(res.elements())
intersection-of-two-arrays-ii
Python3 Beat 100% concise version
JasperZhou
2
251
intersection of two arrays ii
350
0.556
Easy
6,196
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/2424760/easy-approach-or-python
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: ans=[] for i in nums1: if i in nums2: ans.append(i) nums2.remove(i) return ans
intersection-of-two-arrays-ii
easy approach | python
ayushigupta2409
1
166
intersection of two arrays ii
350
0.556
Easy
6,197
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/2294216/89-memory-efficient-solution-or-Python3-or-Easy-understanding
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: a = [] k=len(nums1) for i in range(k): if(nums1[i] in nums2): a.append(nums1[i]) ind = nums2.index(nums1[i]) nums2[ind]= '_' return a
intersection-of-two-arrays-ii
89% memory efficient solution | Python3 | Easy understanding
AngelaInfantaJerome
1
181
intersection of two arrays ii
350
0.556
Easy
6,198
https://leetcode.com/problems/intersection-of-two-arrays-ii/discuss/2127122/two-pointer-and-hashmap
class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: res = [] nums1, nums2 = sorted(nums1), sorted(nums2) i = j = 0 while i < len(nums1) and j < len(nums2): if nums1[i] == nums2[j]: res.append(nums1[i]) i += 1 j += 1 elif nums1[i] < nums2[j]: i += 1 else: j += 1 return res
intersection-of-two-arrays-ii
two pointer & hashmap
andrewnerdimo
1
234
intersection of two arrays ii
350
0.556
Easy
6,199