Dados 3 arreglos , arr[] , brr[] y crr[] , la tarea es encontrar los elementos comunes en al menos 2 arreglos de los 3 arreglos dados
Ejemplos :
Entrada :arr[] = {1, 1, 3, 2, 4}, brr[] = {2, 3, 5}, crr[] = {3, 6}
Salida : {2, 3}
Explicación : Elementos 2 y 3 aparecen en al menos 2 arreglosEntrada :arr[] = {3, 1}, brr[] = {2, 3}, crr[] = {1, 2}
Salida :{2, 3, 1}
Enfoque : la tarea se puede resolver con la ayuda de HashSet Siga los pasos a continuación para resolver el problema:
- Cree tres hashsets (s1, s2 y s3) para almacenar distintos elementos de las tres arrays y también un HashSet para almacenar distintos elementos de las tres arrays.
- Iterar en el conjunto y verificar los elementos comunes en al menos dos conjuntos (s1, s2), (s1, s3) y (s2, s3).
- Si un elemento cumple la condición anterior, imprima ese elemento.
A continuación se muestra la implementación del código anterior:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; void get(vector<int>& p, vector<int>& c, vector<int>& m) { // Store distinct elements of array arr[] set<int> s1; // Store distinct elements of array brr[] set<int> s2; // Store distinct elements of array crr[] set<int> s3; // Store the distinct elements // of all three arrays set<int> 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<int> 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<int> arr = { 1, 1, 3, 2, 4 }; vector<int> brr = { 2, 3, 5 }; vector<int> 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<Integer> s1 = new HashSet<>(); // Store distinct elements of array brr[] Set<Integer> s2 = new HashSet<>(); // Store distinct elements of array crr[] Set<Integer> s3 = new HashSet<>(); // Store the distinct elements // of all three arrays Set<Integer> 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<Integer> 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<int> s1 = new HashSet<int>(); // Store distinct elements of array brr[] HashSet<int> s2 = new HashSet<int>(); // Store distinct elements of array crr[] HashSet<int> s3 = new HashSet<int>(); // Store the distinct elements // of all three arrays HashSet<int> set = new HashSet<int>(); 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
<script> // Javascript implementation of above approach function get(p, c, m) { // Store distinct elements of array arr[] let s1 = new Set(); // Store distinct elements of array brr[] let s2 = new Set(); // Store distinct elements of array crr[] let s3 = new Set(); // Store the distinct elements // of all three arrays let set = new Set(); for (i of p) { s1.add(i); set.add(i); } for (i of c) { s2.add(i); set.add(i); } for (i of m) { s3.add(i); set.add(i); } // Stores the common elements let result = []; for (i of [...set].reverse()) { if (s1.has(i) && s2.has(i) || s2.has(i) && s3.has(i) || s1.has(i) && s3.has(i)) document.write(i + " "); } } // Driver Code let arr = [1, 1, 3, 2, 4]; let brr = [2, 3, 5]; let crr = [3, 6]; get(arr, brr, crr); // This code is contributed by gfgking </script>
2 3
Complejidad de tiempo : O(n1+n2+n3) (donde n1, n2 y n3 son la longitud de las arrays dadas)
Espacio auxiliar : O(n1+n2+n3)
Publicación traducida automáticamente
Artículo escrito por iramkhalid24 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA