LeetCode in Kotlin

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:

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:

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:

Solution

class Solution {
    fun twoOutOfThree(nums1: IntArray, nums2: IntArray, nums3: IntArray): List<Int> {
        val ans: MutableSet<Int> = HashSet()
        val set1: MutableSet<Int> = HashSet()
        for (i in nums1) {
            set1.add(i)
        }
        val set2: MutableSet<Int> = HashSet()
        for (i in nums2) {
            set2.add(i)
        }
        val set3: MutableSet<Int> = HashSet()
        for (i in nums3) {
            set3.add(i)
        }
        for (j in nums1) {
            if (set2.contains(j) || set3.contains(j)) {
                ans.add(j)
            }
        }
        for (j in nums2) {
            if (set1.contains(j) || set3.contains(j)) {
                ans.add(j)
            }
        }
        for (j in nums3) {
            if (set1.contains(j) || set2.contains(j)) {
                ans.add(j)
            }
        }
        val result: MutableList<Int> = ArrayList()
        result.addAll(ans)
        return result
    }
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy