nums1
, nums2
, and nums3
, return a distinct array containing all the values that are present in at least two out of the three arrays. You may return the values in any order.
Example 1:
Input: nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3] Output: [3,2] Explanation: The values that are present in at least two arrays are: - 3, in all three arrays. - 2, in nums1 and nums2.
Example 2:
Input: nums1 = [3,1], nums2 = [2,3], nums3 = [1,2] Output: [2,3,1] Explanation: The values that are present in at least two arrays are: - 2, in nums2 and nums3. - 3, in nums1 and nums2. - 1, in nums1 and nums3.
Example 3:
Input: nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5] Output: [] Explanation: No value is present in at least two arrays.
Constraints:
1 <= nums1.length, nums2.length, nums3.length <= 100
1 <= nums1[i], nums2[j], nums3[k] <= 100
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for finding numbers that appear in at least two out of three lists is to examine every possible number in each list and compare them across all the lists. This involves checking each number to see if it exists in at least two lists.
Here's how the algorithm would work step-by-step:
def twoOutOfThree(first_list, second_list, third_list):
result = set()
# Iterate through the first list
for number_from_first_list in first_list:
count = 0
if number_from_first_list in second_list:
count += 1
if number_from_first_list in third_list:
count += 1
if count >= 1:
result.add(number_from_first_list)
# Iterate through the second list
for number_from_second_list in second_list:
count = 0
# Ensures we account for items in at least two lists
if number_from_second_list in first_list:
count += 1
if number_from_second_list in third_list:
count += 1
if count >= 1:
result.add(number_from_second_list)
# Iterate through the third list
for number_from_third_list in third_list:
count = 0
if number_from_third_list in first_list:
count += 1
if number_from_third_list in second_list:
count += 1
# Add the number to the result if it's in at least two lists
if count >= 1:
result.add(number_from_third_list)
numbers_in_at_least_two_lists = []
for number in result:
first_count = first_list.count(number)
second_count = second_list.count(number)
third_count = third_list.count(number)
if (first_count > 0 and second_count > 0) or \
(first_count > 0 and third_count > 0) or \
(second_count > 0 and third_count > 0):
numbers_in_at_least_two_lists.append(number)
return numbers_in_at_least_two_lists
The goal is to identify numbers that appear in at least two of the given three lists. We can achieve this efficiently by tracking which lists each number appears in, and then checking if any number meets the requirement.
Here's how the algorithm would work step-by-step:
def two_out_of_three(nums1, nums2, nums3):
list_appearances = {}
# Track appearance of numbers in the first list.
for number in nums1:
if number not in list_appearances:
list_appearances[number] = [1, 0, 0]
else:
list_appearances[number][0] = 1
# Track appearance of numbers in the second list.
for number in nums2:
if number not in list_appearances:
list_appearances[number] = [0, 1, 0]
else:
list_appearances[number][1] = 1
# Track appearance of numbers in the third list.
for number in nums3:
if number not in list_appearances:
list_appearances[number] = [0, 0, 1]
else:
list_appearances[number][2] = 1
result = []
# Identify numbers present in at least two lists.
for number, appearance in list_appearances.items():
if sum(appearance) >= 2:
result.append(number)
return result
Case | How to Handle |
---|---|
Null or empty input arrays | Return an empty list if any of the input arrays are null or empty. |
Arrays with duplicate numbers within each array | Use sets to remove duplicates within each array before processing. |
Large input arrays that could cause memory issues | Use efficient data structures like sets and minimize memory usage. |
Integer overflow when calculating frequency counts | Employ appropriate data types like long to prevent integer overflow. |
All three arrays are identical | The set operations will still function correctly, yielding elements present in at least two. |
One array contains all elements from the other two | The set operations correctly identify elements present in at least two arrays. |
Arrays containing negative numbers | The solution should handle negative numbers correctly as hashset do support negative values. |
Arrays containing zero | The solution should handle zero correctly as hashset do support zero value. |