Two Out of Three
We have three integer arrays, and we want to find all the unique values that are present in at least two out of the three arrays.
Here’s a simple approach:
Steps
- Create three sets, one for each array, containing the unique elements of the respective array.
- Create a dictionary to keep track of the occurrence count for each unique value across the three sets.
- Iterate through the three sets, incrementing the count for each unique value in the dictionary.
- Iterate through the dictionary, and if the count for any value is greater than or equal to 2, add it to the result list.
- Return the result list containing the distinct values that are present in at least two out of the three arrays.
Code
Here’s the code:
|
|
Explanation
The code takes advantage of sets and dictionaries to efficiently find the unique values that are present in at least two arrays. By keeping track of the occurrence count in a dictionary, we can quickly identify which values meet the criteria. The use of sets ensures that we only consider the distinct values in each array, and the final result list will also contain only distinct values.