Dadas dos arrays (que pueden o no estar ordenadas) de tamaños M y N respectivamente. Hay arreglos que pueden tener algunos elementos comunes en ellos. Debe contar la cantidad de elementos cuyas ocurrencias están más en la primera array que en la segunda.
Ejemplos:
Entrada: arr1[] = {41, 43, 45, 50}, M = 4
arr2[] = {55, 67, 65, 61, 62}, N = 5
Salida: 4
Explicación:
arr1[] contiene 41, 43 , 45, 50 con frecuencia, todos los que tienen 1
arr2[] no contienen ningún elemento que sea común en ambos, por lo tanto, el recuento será 4Entrada: arr1[] = {40, 45, 56, 60}, M = 4
arr2[] = {40, 45, 56, 58}, N = 4
Salida: 1
Explicación:
arr1[] contiene 40, 45, 56 , 60 con frecuencia, todos con 1
arr2[] contiene 3 elementos en común con arr1[] que son 40, 45, 56
pero no 60, por lo que el recuento se convierte en 1
La idea es usar un mapa hash y mantener el recuento de la frecuencia de los números en la primera array. Mientras atraviesa la segunda array, reduzca el recuento en el mapa para cada número entero encontrado. Ahora cuente los elementos cuya frecuencia es mayor que 0, de lo contrario, devuelva 0.
C++
// C++ to count number of elements present in arr1 whose // occurrence is more than in arr2 #include <bits/stdc++.h> using namespace std; int Largercount(int arr1[], int arr2[], int m, int n) { bool f = false; int count = 0; // map to store frequency of elements present in arr1 unordered_map<int, int> mp; // frequency of elements of arr1 is calculated for (int i = 0; i < m; i++) mp[arr1[i]]++; // check if the elements of arr2 // is present in arr2 or not for (int i = 0; i < n; i++) if (mp.find(arr2[i]) != mp.end() && mp[arr2[i]] != 0) mp[arr2[i]]--; // count the elements of arr1 whose // frequency is more than arr2 for (int i = 0; i < m; i++) { if (mp[arr1[i]] != 0) { count++; mp[arr1[i]] = 0; } } return count; } // Driver code int main() { int arr1[] = { 2, 4, 4, 6, 6, 6, 8, 9 }; int arr2[] = { 2, 2, 4, 6, 6 }; cout << Largercount(arr1, arr2, 8, 5); return 0; }
Java
// Java to count number of elements present in arr1 whose // occurrence is more than in arr2 import java.util.*; import java.lang.*; import java.io.*; class GFG { public static int Largercount(int arr1[], int arr2[], int m, int n) { int count = 0; // map to store frequency of elements present in arr1 HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>(); // frequency of elements of arr1 is calculated for (int i = 0; i < m; i++) { int key = arr1[i]; if (mp.containsKey(key)) { int freq = mp.get(key); freq++; mp.put(key, freq); } else mp.put(key, 1); } // check if the elements of arr2 is present in arr2 or not for (int i = 0; i < n; i++) { if (mp.containsKey(arr2[i]) && mp.get(arr2[i]) != 0) { int freq = mp.get(arr2[i]); freq--; mp.put(arr2[i], freq); } } // count the elements of arr1 whose // frequency is more than arr2 for (int i = 0; i < m; i++) { if (mp.get(arr1[i]) != 0) { count++; mp.put(arr1[i], 0); } } return count; } // Driver code public static void main(String[] args) { int arr1[] = new int[] { 2, 4, 4, 6, 6, 6, 8, 9}; int arr2[] = new int[] { 2, 2, 4, 6, 6 }; System.out.print(Largercount(arr1, arr2, 8, 5)); } }
Python3
# Python3 to count number of elements # present in arr1 whose occurrence is # more than in arr2 def Largercount(arr1, arr2, m, n): count = 0 # map to store frequency of # elements present in arr1 mp=dict() # frequency of elements of arr1 # is calculated for i in range(m): mp[arr1[i]] = mp.get(arr1[i], 0) + 1 # check if the elements of arr2 # is present in arr2 or not for i in range(n): if (arr2[i] in mp.keys() and mp[arr2[i]] != 0): mp[arr2[i]] -= 1 # count the elements of arr1 whose # frequency is more than arr2 for i in range(m): if (mp[arr1[i]] != 0): count += 1 mp[arr1[i]] = 0 return count # Driver code arr1 = [2, 4, 4, 6, 6, 6, 8, 9] arr2 = [2, 2, 4, 6, 6 ] print(Largercount(arr1, arr2, 8, 5)) # This code is contributed by mohit kumar
C#
// C# to count number of elements // present in arr1 whose occurrence // is more than in arr2 using System; using System.Collections.Generic; class GFG { public static int Largercount(int[] arr1, int[] arr2, int m, int n) { int count = 0; // map to store frequency of // elements present in arr1 Dictionary<int, int> mp = new Dictionary<int, int>(); // frequency of elements // of arr1 is calculated for (int i = 0; i < m; i++) { int key = arr1[i]; if (mp.ContainsKey(key)) { int freq = mp[key]; freq++; mp[key] = freq; } else { mp[key] = 1; } } // check if the elements of arr2 // is present in arr2 or not for (int i = 0; i < n; i++) { if (mp.ContainsKey(arr2[i]) && mp[arr2[i]] != 0) { int freq = mp[arr2[i]]; freq--; mp[arr2[i]] = freq; } } // count the elements of arr1 whose // frequency is more than arr2 for (int i = 0; i < m; i++) { if (mp[arr1[i]] != 0) { count++; mp[arr1[i]] = 0; } } return count; } // Driver code public static void Main(string[] args) { int[] arr1 = new int[] {2, 4, 4, 6, 6, 6, 8, 9}; int[] arr2 = new int[] {2, 2, 4, 6, 6}; Console.Write(Largercount(arr1, arr2, 8, 5)); } } // This code is contributed by Shrikant13
Javascript
<script> // Javascript to count number of elements present in arr1 whose // occurrence is more than in arr2 function Largercount(arr1,arr2,m,n) { let count = 0; // map to store frequency of elements present in arr1 let mp = new Map(); // frequency of elements of arr1 is calculated for (let i = 0; i < m; i++) { let key = arr1[i]; if (mp.has(key)) { let freq = mp.get(key); freq++; mp.set(key, freq); } else mp.set(key, 1); } // check if the elements of arr2 is present in arr2 or not for (let i = 0; i < n; i++) { if (mp.has(arr2[i]) && mp.get(arr2[i]) != 0) { let freq = mp.get(arr2[i]); freq--; mp.set(arr2[i], freq); } } // count the elements of arr1 whose // frequency is more than arr2 for (let i = 0; i < m; i++) { if (mp.get(arr1[i]) != 0) { count++; mp.set(arr1[i], 0); } } return count; } // Driver code let arr1=[2, 4, 4, 6, 6, 6, 8, 9]; let arr2=[2, 2, 4, 6, 6 ]; document.write(Largercount(arr1, arr2, 8, 5)); // This code is contributed by unknown2108 </script>
4
Complejidad temporal: O(m + n)
Publicación traducida automáticamente
Artículo escrito por Kirti_Mangal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA