📌  相关文章
📜  查找三个数组中至少两个中存在的数字

📅  最后修改于: 2022-05-13 01:56:06.491000             🧑  作者: Mango

查找三个数组中至少两个中存在的数字

给定 3 个数组arr[]brr[]crr[] ,任务是在给定的 3 个数组中找到至少 2 个数组的共同元素
例子

方法:可以在HashSet的帮助下解决任务按照以下步骤解决问题:

  • 创建三个哈希集(s1、s2 和 s3)来存储三个数组的不同元素,还创建一个哈希集来存储所有三个数组的不同元素。
  • 迭代集合并检查至少两个集合(s1,s2),(s1,s3)和(s2,s3)中的公共元素。
  • 如果元素满足上述条件,则打印该元素。

下面是上述代码的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
void get(vector& p, vector& c, vector& m)
{
 
    // Store distinct elements of array arr[]
    set s1;
 
    // Store distinct elements of array brr[]
    set s2;
 
    // Store distinct elements of array crr[]
    set s3;
 
    // Store the distinct elements
    // of all three arrays
    set set;
    for (auto i : p) {
        s1.insert(i);
        set.insert(i);
    }
 
    for (auto i : c) {
        s2.insert(i);
        set.insert(i);
    }
 
    for (int i : m) {
        s3.insert(i);
        set.insert(i);
    }
 
    // Stores the common elements
    vector result;
    for (auto i : set) {
        if (s1.count(i) && s2.count(i)
            || s2.count(i) && s3.count(i)
            || s1.count(i) && s3.count(i))
            cout << i << " ";
    }
}
 
// Driver Code
int main()
{
    vector arr = { 1, 1, 3, 2, 4 };
    vector brr = { 2, 3, 5 };
    vector crr = { 3, 6 };
    get(arr, brr, crr);
 
    return 0;
}
 
    // This code is contributed by rakeshsahni


Java
// Java implementation of above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
    public static void get(
        int[] p, int[] c, int[] m)
    {
 
        // Store distinct elements of array arr[]
        Set s1 = new HashSet<>();
 
        // Store distinct elements of array brr[]
        Set s2 = new HashSet<>();
 
        // Store distinct elements of array crr[]
        Set s3 = new HashSet<>();
 
        // Store the distinct elements
        // of all three arrays
        Set set = new HashSet<>();
        for (int i : p) {
            s1.add(i);
            set.add(i);
        }
 
        for (int i : c) {
            s2.add(i);
            set.add(i);
        }
 
        for (int i : m) {
            s3.add(i);
            set.add(i);
        }
 
        // Stores the common elements
        List result
            = new ArrayList<>();
        for (int i : set) {
            if (s1.contains(i)
                    && s2.contains(i)
                || s2.contains(i)
                       && s3.contains(i)
                || s1.contains(i)
                       && s3.contains(i))
                System.out.print(i + " ");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 1, 3, 2, 4 };
        int[] brr = { 2, 3, 5 };
        int[] crr = { 3, 6 };
        get(arr, brr, crr);
    }
}


Python3
# Python3 implementation of above approach
def get(p, c, m) :
 
    # Store distinct elements of array arr[]
    s1 = set();
 
    # Store distinct elements of array brr[]
    s2 = set();
 
    # Store distinct elements of array crr[]
    s3 = set();
 
    # Store the distinct elements
    # of all three arrays
    set_obj = set();
     
    for i in p :
        s1.add(i);
        set_obj.add(i);
 
    for i in c :
        s2.add(i);
        set_obj.add(i);
 
    for i in m :
        s3.add(i);
        set_obj.add(i);
 
    # Stores the common elements
    result = [];
    for i in set_obj :
        if (list(s1).count(i) and list(s2).count(i)
            or list(s2).count(i) and list(s3).count(i)
            or list(s1).count(i) and list(s3).count(i)) :
            print(i,end= " ");
   
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 1, 1, 3, 2, 4 ];
    brr = [ 2, 3, 5 ];
    crr = [ 3, 6 ];
    get(arr, brr, crr);
 
    # This code is contributed by AnkThon


C#
// C# implementation of above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG {
    static void get(
        int[] p, int[] c, int[] m)
    {
 
        // Store distinct elements of array arr[]
        HashSet s1 = new HashSet();
 
        // Store distinct elements of array brr[]
        HashSet s2 = new HashSet();
 
        // Store distinct elements of array crr[]
        HashSet s3 = new HashSet();
 
        // Store the distinct elements
        // of all three arrays
        HashSet set = new HashSet();
        foreach (int i in p) {
            s1.Add(i);
            set.Add(i);
        }
 
        foreach (int i in c) {
            s2.Add(i);
            set.Add(i);
        }
 
        foreach (int i in m) {
            s3.Add(i);
            set.Add(i);
        }
 
        // Stores the common elements
        ArrayList result
            = new ArrayList();
        foreach (int i in set) {
            if (s1.Contains(i)
                    && s2.Contains(i)
                || s2.Contains(i)
                       && s3.Contains(i)
                || s1.Contains(i)
                       && s3.Contains(i))
                Console.Write(i + " ");
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 1, 1, 3, 2, 4 };
        int[] brr = { 2, 3, 5 };
        int[] crr = { 3, 6 };
        get(arr, brr, crr);
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
2 3 

时间复杂度:O(n1+n2+n3)(其中 n1、n2 和 n3 是给定数组的长度)
辅助空间:O(n1+n2+n3)