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 <= 1001 <= nums1[i], nums2[j], nums3[k] <= 100Problem Overview: You receive three integer arrays nums1, nums2, and nums3. The task is to return all distinct values that appear in at least two of the arrays. Duplicates inside the same array should not inflate the count; only presence across different arrays matters.
Approach 1: Using Sets to Store Unique Elements and Count Occurrences (Time: O(n + m + k), Space: O(n + m + k))
The most common solution uses set structures to remove duplicates inside each array first. Convert each array into a set, then track how many arrays contain each number using a hash map. Iterate through each set and increment a counter for its elements. Any value with a count of at least two appears in two or more arrays and belongs in the result.
This approach works well because set lookups and insertions are O(1) on average. By eliminating duplicates early, you avoid incorrectly counting repeated numbers from the same array. The technique relies heavily on hash table behavior and is the most readable solution for interviews.
Approach 2: Using Frequency Arrays (Time: O(n + m + k), Space: O(U))
If the value range is reasonably bounded, a frequency array can replace the hash map. First convert each input array into a set so duplicates inside one array are ignored. Then maintain a frequency array where the index represents a value and the stored number represents how many arrays contain it. Traverse the three sets and increment the frequency for each value.
After processing all sets, iterate through the frequency structure and collect numbers with frequency ≥ 2. This avoids hashing overhead and can be slightly faster in languages where array access is cheaper than map operations. The tradeoff is extra memory proportional to the maximum possible value, which may not be ideal for large ranges. This approach still relies on concepts from array processing and counting techniques often seen in bit manipulation or frequency problems.
Recommended for interviews: The set + hash map method is what most interviewers expect. It demonstrates that you handle duplicates correctly and leverage constant‑time hash lookups. Mentioning the frequency-array optimization shows deeper understanding of tradeoffs when the input range is known. Both run in linear time, but the hash-based approach is safer when constraints are large or unknown.
To solve the problem, we'll use sets to capture unique elements from each array, then count the total appearances of each element across all arrays. Finally, we'll select elements that appear in at least two arrays.
The approach involves converting each array into a set to fetch distinct elements. We then use a dictionary (or map) to track the frequency of each element appearing across the three sets.
The function twoOutOfThree creates sets for each input list to get unique values from them. It uses a dictionary to count how many sets each element appears in. Finally, it returns a list of elements that appear in at least two sets.
Time Complexity: O(n), where n is the total number of elements across all input lists since we iterate through each element once.
Space Complexity: O(u), where u is the number of unique elements given the constraints.
This method employs auxiliary arrays to record the presence frequency of elements. Given the constraint that elements are between 1 and 100, we can use a fixed-size frequency array to track how many of the three arrays contain any given element.
This Python solution uses a frequency array of size 101 to count how many times a number is present in the three arrays. By marking the frequency for unique elements in each list, the solution considers elements appearing in at least two arrays.
Python
Java
JavaScript
C#
C++
Time Complexity: O(n), where n is the sum of lengths of nums1, nums2, and nums3.
Space Complexity: O(100), which is effectively O(1).
We can first put each element of the arrays into an array, then enumerate each number i from 1 to 100, and check whether i appears in at least two arrays. If so, add i to the answer array.
The time complexity is O(n_1 + n_2 + n_3), and the space complexity is O(n_1 + n_2 + n_3). Here, n_1, n_2, n_3 are the lengths of the arrays nums1, nums2, and nums3, respectively.
| Approach | Complexity |
|---|---|
| Using Sets to Store Unique Elements and Count Occurrences | Time Complexity: |
| Using Frequency Arrays | Time Complexity: |
| Array + Enumeration | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Sets + Hash Map Counting | O(n + m + k) | O(n + m + k) | General case. Works for any value range and is the most common interview solution. |
| Frequency Array Counting | O(n + m + k) | O(U) | Useful when value range is small and predictable, making array indexing faster than hashing. |
Leetcode Weekly Contest 262 | 2032. Two Out of Three(Easy) in English • Competitive Somya • 1,530 views views
Watch 9 more video solutions →Practice Two Out of Three with our built-in code editor and test cases.
Practice on FleetCode