Problem Description
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.
Key Insights
- Convert each array into a set to eliminate duplicate values within the same array.
- Use a hashmap or frequency counter to count the occurrence of each distinct number across the three sets.
- Include the number in the result if its frequency is at least 2.
- The ordering of output does not matter.
Space and Time Complexity
Time Complexity: O(n1 + n2 + n3) where n1, n2, and n3 are the lengths of the three arrays. Space Complexity: O(n1 + n2 + n3) for the sets and the frequency counter.
Solution
We start by converting each input array into a set to remove duplicates. Then, we use a hashmap (or dictionary) to count how many sets each distinct number appears in. Finally, we iterate through the hashmap and select the numbers that appear in at least two out of the three sets. This approach efficiently handles the task by reducing the number of elements to consider and directly counting their occurrences.