Skip to main content

Two Out of Three - Solution & Explanation

EasyArrayHash TableBit Manipulation16 min readAsked at: Booking.com, Info Edge
Practice this problem

Problem Statement

Given three integer arrays 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

Approach Overview

Problem 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.

Approach 1: Using Sets to Store Unique Elements and Count Occurrences

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.

Code

Python

Java

C

JavaScript

C#

C++

Complexity

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.

Try this approach in the editor →

Approach 2: Using Frequency Arrays

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.

Code

Python

Java

JavaScript

C#

C++

Complexity

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).

Try this approach in the editor →

Approach 3: Array + Enumeration

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.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using Sets to Store Unique Elements and Count Occurrences

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.

Using Frequency Arrays

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).

Array + Enumeration—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Sets + Hash Map CountingO(n + m + k)O(n + m + k)General case. Works for any value range and is the most common interview solution.
Frequency Array CountingO(n + m + k)O(U)Useful when value range is small and predictable, making array indexing faster than hashing.

Video Solution

Leetcode Weekly Contest 262 | 2032. Two Out of Three(Easy) in English • Competitive Somya • 1,549 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Two Out of Three easy or hard?
Two Out of Three is classified as an Easy problem. The challenge mainly tests understanding of sets, hash tables, and duplicate handling. Once you recognize that duplicates inside the same array must be ignored, the solution becomes straightforward.
How to solve Two Out of Three in O(n)?
Use three sets to remove duplicates from each array. Track element presence using a hash map or frequency array. Increment the count for each number appearing in a set, and collect numbers with a count of at least two. Since every element is processed once, the total runtime is linear.
What is the best approach for Two Out of Three?
The most practical approach uses sets combined with a hash map. Convert each array into a set to remove duplicates, then count how many arrays contain each value. Any number appearing in at least two sets is added to the result. This runs in O(n + m + k) time with hash lookups.
Is Two Out of Three asked at Google/Amazon/Meta?
Problems involving set intersections and hash-based counting frequently appear in interviews at companies like Amazon and Meta. While this exact question may vary, the pattern of deduplicating arrays and counting occurrences across collections is common in coding interviews.
What data structure is used in Two Out of Three?
The primary data structures are hash sets and hash maps. Sets ensure duplicates inside a single array are ignored, while a hash map tracks how many arrays contain each value. Some implementations replace the map with a frequency array when the number range is limited.
What is the time complexity of Two Out of Three?
The optimal solution runs in O(n + m + k) time, where n, m, and k are the lengths of the three arrays. Each array is processed once to build sets and update counts. Hash map operations are O(1) on average, keeping the overall complexity linear.
Two Out of Three Python or Java solution approach?
Both Python and Java implementations typically convert arrays into sets and use a dictionary or HashMap to count occurrences. Iterate through each set and increment the count for its elements. Finally, return all numbers whose count is at least two.

Ready to solve this problem?

Practice Two Out of Three with our built-in code editor and test cases.

Practice on FleetCode