📌  相关文章
📜  从数组中删除出现超过 k 次的元素

📅  最后修改于: 2021-10-27 07:46:34             🧑  作者: Mango

给定一个整数数组,删除所有出现在数组中严格超过 k 次的元素。
例子:

Input : arr[] = {1, 2, 2, 3, 2, 3, 4}
        k = 2
Output : 1 3 3 4

Input : arr[] = {2, 5, 5, 7}
        k = 1
Output : 2 7

方法:

  • 取一个哈希映射,它将存储数组中所有元素的频率。
  • 现在,再次穿越。
  • 打印出现次数小于或等于 k 次的元素。
C++
// C++ program to remove the elements which
// appear more than k times from the array.
#include "iostream"
#include "unordered_map"
using namespace std;
 
void RemoveElements(int arr[], int n, int k)
{
    // Hash map which will store the
    // frequency of the elements of the array.
    unordered_map mp;
 
    for (int i = 0; i < n; ++i) {
        // Incrementing the frequency
        // of the element by 1.
        mp[arr[i]]++;
    }
 
    for (int i = 0; i < n; ++i) {
        // Print the element which appear
        // less than or equal to k times.
        if (mp[arr[i]] <= k) {
            cout << arr[i] << " ";
        }
    }
}
 
int main(int argc, char const* argv[])
{
    int arr[] = { 1, 2, 2, 3, 2, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int k = 2;
 
    RemoveElements(arr, n, k);
    return 0;
}


Java
// Java program to remove the elements which
// appear more than k times from the array.
import java.util.HashMap;
import java.util.Map;
 
class GFG
{
 
static void RemoveElements(int arr[], int n, int k)
{
    // Hash map which will store the
    // frequency of the elements of the array.
    Map mp = new HashMap<>();
 
    for (int i = 0; i < n; ++i)
    {
        // Incrementing the frequency
        // of the element by 1.
        mp.put(arr[i],mp.get(arr[i]) == null?1:mp.get(arr[i])+1);
 
    }
 
    for (int i = 0; i < n; ++i)
    {
        // Print the element which appear
        // less than or equal to k times.
        if (mp.containsKey(arr[i]) && mp.get(arr[i]) <= k)
        {
            System.out.print(arr[i] + " ");
        }
    }
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 2, 3, 2, 3, 4 };
    int n = arr.length;
 
    int k = 2;
 
    RemoveElements(arr, n, k);
     
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python 3 program to remove the elements which
# appear more than k times from the array.
def RemoveElements(arr, n, k):
     
    # Hash map which will store the
    # frequency of the elements of the array.
    mp = {i:0 for i in range(len(arr))}
 
    for i in range(n):
         
        # Incrementing the frequency
        # of the element by 1.
        mp[arr[i]] += 1
 
    for i in range(n):
         
        # Print the element which appear
        # less than or equal to k times.
        if (mp[arr[i]] <= k):
            print(arr[i], end = " ")
 
# Driver Code   
if __name__ == '__main__':
    arr = [1, 2, 2, 3, 2, 3, 4]
    n = len(arr)
 
    k = 2
 
    RemoveElements(arr, n, k)
 
# This code is contributed by
# Sahil_Shelangia


C#
// C# program to remove the elements which
// appear more than k times from the array.
using System;
using System.Collections.Generic;
 
class GFG
{
static void RemoveElements(int [] arr,
                           int n, int k)
{
    // Hash map which will store the
    // frequency of the elements of the array.
    Dictionary mp = new Dictionary();
 
    for (int i = 0; i < n; ++i)
    {
        // Incrementing the frequency
        // of the element by 1.
        if(mp.ContainsKey(arr[i]))
            mp[arr[i]]++;
        else
            mp[arr[i]] = 1;
    }
 
    for (int i = 0; i < n; ++i)
    {
        // Print the element which appear
        // less than or equal to k times.
        if (mp.ContainsKey(arr[i]) && mp[arr[i]] <= k)
        {
            Console.Write(arr[i] + " ");
        }
    }
}
 
// Driver code
static public void Main()
{
    int [] arr = { 1, 2, 2, 3, 2, 3, 4 };
    int n = arr.Length;
 
    int k = 2;
 
    RemoveElements(arr, n, k);
}
}
 
// This code is contributed by Mohit kumar 29


Javascript


Python3
# Python3 program to remove the elements which
# appear strictly less than k times from the array.
from collections import Counter
 
def removeElements(arr, n, k):
 
    # Calculating frequencies
    # using Counter function
    freq = Counter(arr)
    for i in range(n):
 
        # Print the element which appear
        # more than or equal to k times.
        if (freq[arr[i]] <= k):
            print(arr[i], end=" ")
 
 
# Driver Code
arr = [1, 2, 2, 3, 2, 3, 4]
n = len(arr)
k = 2
removeElements(arr, n, k)
 
# This code is contributed by vikkycirus


输出:

1 3 3 4

时间复杂度– O(N)

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

  • 使用Counter函数计算每个元素的频率
  • 遍历数组。
  • 打印出现次数小于或等于 k 次的元素。

下面是上述方法的实现:

蟒蛇3

# Python3 program to remove the elements which
# appear strictly less than k times from the array.
from collections import Counter
 
def removeElements(arr, n, k):
 
    # Calculating frequencies
    # using Counter function
    freq = Counter(arr)
    for i in range(n):
 
        # Print the element which appear
        # more than or equal to k times.
        if (freq[arr[i]] <= k):
            print(arr[i], end=" ")
 
 
# Driver Code
arr = [1, 2, 2, 3, 2, 3, 4]
n = len(arr)
k = 2
removeElements(arr, n, k)
 
# This code is contributed by vikkycirus

输出:

1 3 3 4