Print all array elements occurring at least M times
Given an array arr[] consisting of N integers and a positive integer M, the task is to find the number of array elements that occur at least M times.
Examples:
Input: arr[] = {2, 3, 2, 2, 3, 5, 6, 3}, M = 2
Output: 2 3
Explanation:
In the given array arr[], the element that occurs at least M number of times are {2, 3}.Input: arr[] = {1, 32, 2, 1, 33, 5, 1, 5}, M = 2
Output: 1 5
Naive Approach: The simplest approach to solve the problem is as follows:
- Traverse the array from left to right
- Check if an element has already appeared in the earlier traversal or not. If appeared check for the next element. Else again traverse the array from ith position to (n – 1)th position.
- If the frequency is >= M. Print the element.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the number of array // elements with frequency at least M void printElements( int arr[], int N, int M) { // Traverse the array for ( int i = 0; i < N; i++) { int j; for (j = i - 1; j >= 0; j--) { if (arr[i] == arr[j]) break ; } // If the element appeared before if (j >= 0) continue ; // Count frequency of the element int freq = 0; for (j = i; j < N; j++) { if (arr[j] == arr[i]) freq++; } if (freq >= M) cout << arr[i] << " " ; } } // Driver Code int main() { int arr[] = { 2, 3, 2, 2, 3, 5, 6, 3 }; int M = 2; int N = sizeof (arr) / sizeof (arr[0]); printElements(arr, N, M); return 0; } |
Java
// Java program for the above approach import java.io.*; class GFG{ // Function to find the number of array // elements with frequency at least M static void printElements( int [] arr, int N, int M) { // Traverse the array for ( int i = 0 ; i < N; i++) { int j; for (j = i - 1 ; j >= 0 ; j--) { if (arr[i] == arr[j]) break ; } // If the element appeared before if (j >= 0 ) continue ; // Count frequency of the element int freq = 0 ; for (j = i; j < N; j++) { if (arr[j] == arr[i]) freq++; } if (freq >= M) System.out.print(arr[i] + " " ); } } // Driver Code public static void main(String[] args) { int [] arr = { 2 , 3 , 2 , 2 , 3 , 5 , 6 , 3 }; int M = 2 ; int N = arr.length; printElements(arr, N, M); } } // This code is contributed by subhammahato348 |
Python3
# Python3 program for the above approach # Function to find the number of array # elements with frequency at least M def printElements(arr, N, M): # Traverse the array for i in range (N): j = i - 1 while j > = 0 : if (arr[i] = = arr[j]): break j - = 1 # If the element appeared before if (j > = 0 ): continue # Count frequency of the element freq = 0 for j in range (i, N): if (arr[j] = = arr[i]): freq + = 1 if (freq > = M): print (arr[i], end = " " ) # Driver Code arr = [ 2 , 3 , 2 , 2 , 3 , 5 , 6 , 3 ] M = 2 N = len (arr) printElements(arr, N, M) # This code is contributed by rohitsingh07052 |
C#
// C# program for the above approach using System; class GFG{ // Function to find the number of array // elements with frequency at least M static void printElements( int [] arr, int N, int M) { // Traverse the array for ( int i = 0; i < N; i++) { int j; for (j = i - 1; j >= 0; j--) { if (arr[i] == arr[j]) break ; } // If the element appeared before if (j >= 0) continue ; // Count frequency of the element int freq = 0; for (j = i; j < N; j++) { if (arr[j] == arr[i]) freq++; } if (freq >= M) Console.Write(arr[i] + " " ); } } // Driver Code public static void Main() { int [] arr = { 2, 3, 2, 2, 3, 5, 6, 3 }; int M = 2; int N = arr.Length; printElements(arr, N, M); } } // This code is contributed by subham348 |
Javascript
<script> // Javascript program for the above approach // Function to find the number of array // elements with frequency at least M function printElements(arr, N, M) { // Traverse the array for (let i = 0; i < N; i++) { let j; for (j = i - 1; j >= 0; j--) { if (arr[i] == arr[j]) break ; } // If the element appeared before if (j >= 0) continue ; // Count frequency of the element let freq = 0; for (j = i; j < N; j++) { if (arr[j] == arr[i]) freq++; } if (freq >= M) document.write(arr[i] + " " ); } } // Driver Code let arr = [ 2, 3, 2, 2, 3, 5, 6, 3 ]; let M = 2; let N = arr.length; printElements(arr, N, M); // This code is contributed by subham348. </script> |
2 3
Approach: The given problem can be solved by storing the frequencies of array elements in a HashMap, say M, and print all the elements in the map having frequency at least M.
Below is the implementation of the above approach.
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the number of array // elements with frequency at least M void printElements( int arr[], int N, int M) { // Stores the frequency of each // array elements unordered_map< int , int > freq; // Traverse the array for ( int i = 0; i < N; i++) { // Update frequency of // current array element freq[arr[i]]++; } // Traverse the map and print array // elements occurring at least M times for ( auto it : freq) { if (it.second >= M) { cout << it.first << " " ; } } return ; } // Driver Code int main() { int arr[] = { 2, 3, 2, 2, 3, 5, 6, 3 }; int M = 2; int N = sizeof (arr) / sizeof (arr[0]); printElements(arr, N, M); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to find the number of array // elements with frequency at least M static void printElements( int arr[], int N, int M) { // Stores the frequency of each // array elements HashMap<Integer, Integer> freq = new HashMap<>(); // Traverse the array for ( int i = 0 ; i < N; i++) { // Update frequency of // current array element freq.put(arr[i], freq.getOrDefault(arr[i], 0 ) + 1 ); } // Traverse the map and print array // elements occurring at least M times for ( int key : freq.keySet()) { if (freq.get(key) >= M) { System.out.print(key + " " ); } } } // Driver Code public static void main(String[] args) { int arr[] = { 2 , 3 , 2 , 2 , 3 , 5 , 6 , 3 }; int M = 2 ; int N = arr.length; printElements(arr, N, M); } } // This code is contributed by Kingash. |
Python3
# Python3 program for the above approach # Function to find the number of array # elements with frequency at least M def printElements(arr, N, M): # Stores the frequency of each # array elements freq = {} # Traverse the array for i in arr: # Update frequency of # current array element freq[i] = freq.get(i, 0 ) + 1 # Traverse the map and prarray # elements occurring at least M times for it in freq: if (freq[it] > = M): print (it, end = " " ) return # Driver Code if __name__ = = '__main__' : arr = [ 2 , 3 , 2 , 2 , 3 , 5 , 6 , 3 ] M = 2 N = len (arr) printElements(arr, N, M) # This code is contributed by mohit kumar 29. |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to find the number of array // elements with frequency at least M static void printElements( int []arr, int N, int M) { // Stores the frequency of each // array elements Dictionary< int , int > freq = new Dictionary< int , int >(); // Traverse the array for ( int i = 0; i < N; i++) { // Update frequency of // current array element if (freq.ContainsKey(arr[i])) freq[arr[i]] += 1; else freq[arr[i]] = 1; } // Traverse the map and print array // elements occurring at least M times foreach ( var item in freq) { if (item.Value >= M) { Console.Write(item.Key + " " ); } } return ; } // Driver Code public static void Main() { int []arr = { 2, 3, 2, 2, 3, 5, 6, 3 }; int M = 2; int N = arr.Length; printElements(arr, N, M); } } // This code is contributed by SURENDRA_GANGWAR |
Javascript
<script> // Javascript program for the above approach // Function to find the number of array // elements with frequency at least M function printElements(arr, N, M) { // Stores the frequency of each // array elements let freq = new Map(); // Traverse the array for (let i = 0; i < N; i++) { // Update frequency of // current array element freq[arr[i]]++; if (freq.has(arr[i])) { freq.set(arr[i], freq.get(arr[i]) + 1) } else { freq.set(arr[i], 1) } } // Traverse the map and print array // elements occurring at least M times for (let it of freq) { if (it[1] >= M) { document.write(it[0] + " " ); } } return ; } // Driver Code let arr = [2, 3, 2, 2, 3, 5, 6, 3]; let M = 2; let N = arr.length; printElements(arr, N, M); // This code is contributed by gfgking. </script> |
2 3
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #2:Using Built in python functions:
- Count the frequencies of every element using Counter() function
- Traverse the frequency array and print all the elements which occur at least m times.
Below is the implementation:
Python3
# Python3 implementation from collections import Counter # Function to find the number of array # elements with frequency at least M def printElements(arr, M): # Counting frequency of every element using Counter mp = Counter(arr) # Traverse the map and print all # the elements with occurrence atleast m times for it in mp: if mp[it] > = M: print (it, end = " " ) # Driver code arr = [ 2 , 3 , 2 , 2 , 3 , 5 , 6 , 3 ] M = 2 printElements(arr, M) # This code is contributed by vikkycirus |
2 3
Time Complexity: O(N)
Auxiliary Space: O(N)