📌  相关文章
📜  打印出现至少M次的所有数组元素

📅  最后修改于: 2021-05-13 23:25:31             🧑  作者: Mango

给定一个由N个整数和一个正整数M组成的数组arr [] ,任务是查找至少M次出现的数组元素的数量。

例子:

天真的方法:解决问题的最简单方法如下:

  • 从左到右遍历数组
  • 检查元素是否已经出现在较早的遍历中。如果出现,请检查下一个元素。否则,再次从第i个位置遍历到(n – 1)个位置。
  • 如果频率> = M。打印元素。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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


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


C++
// C++ program for the above approach
 
#include 
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 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 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 freq = new Dictionary();
 
    // 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


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 

方法:可以通过将数组元素的频率存储在HashMap中(例如M )并打印频率至少为M的映射中的所有元素来解决给定的问题。

下面是上述方法的实现。

C++

// C++ program for the above approach
 
#include 
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 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 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 freq = new Dictionary();
 
    // 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
输出
2 3 

时间复杂度: O(N)
辅助空间: O(N)

方法2:使用内置的Python函数:

  • 使用Counter()函数计算每个元素的频率
  • 遍历频率阵列并打印所有出现至少m次的元素。

下面是实现:

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 

时间复杂度: O(N)

辅助空间: O(N)