All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2001_2100.s2032_two_out_of_three.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g2001_2100.s2032_two_out_of_three;

// #Easy #Array #Hash_Table #2022_05_25_Time_9_ms_(45.56%)_Space_47.4_MB_(38.52%)

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 2032 - Two Out of Three\.
 *
 * Easy
 *
 * 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`
**/
public class Solution {
    public List twoOutOfThree(int[] nums1, int[] nums2, int[] nums3) {
        Set ans = new HashSet<>();
        Set set1 = new HashSet<>();
        for (int i : nums1) {
            set1.add(i);
        }
        Set set2 = new HashSet<>();
        for (int i : nums2) {
            set2.add(i);
        }
        Set set3 = new HashSet<>();
        for (int i : nums3) {
            set3.add(i);
        }
        for (int j : nums1) {
            if (set2.contains(j) || set3.contains(j)) {
                ans.add(j);
            }
        }
        for (int j : nums2) {
            if (set1.contains(j) || set3.contains(j)) {
                ans.add(j);
            }
        }
        for (int j : nums3) {
            if (set1.contains(j) || set2.contains(j)) {
                ans.add(j);
            }
        }
        List result = new ArrayList<>();
        result.addAll(ans);
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy