📌  相关文章
📜  从数组中删除所有奇数频率元素

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

给定一个包含大小为N 的整数的数组arr ,任务是从数组中删除所有具有奇数频率的元素。
例子:

方法:

  • 创建一个映射并将数组中每个元素的频率存储到同一个映射中。
  • 然后,遍历数组并借助映射找出哪些元素具有奇数频率。
  • 忽略所有具有奇数频率的元素并打印其余的元素

下面是上述方法的实现:

C++
// C++ program to removes all odd
// frequency elements from an Array
 
#include 
using namespace std;
 
// Function that removes the
// elements which have odd
// frequencies in the array
void remove(int arr[], int n)
{
    // Create a map to store the
    // frequency of each element
    unordered_map m;
    for (int i = 0; i < n; i++) {
        m[arr[i]]++;
    }
 
    // Remove the elements which
    // have odd frequencies
    for (int i = 0; i < n; i++) {
 
        // If the element has
        // odd frequency then skip
        if ((m[arr[i]] & 1))
            continue;
 
        cout << arr[i] << ", ";
    }
}
 
// Driver code
int main()
{
    int arr[]
        = { 3, 3, 3, 2,
            2, 4, 7, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    remove(arr, n);
 
    return 0;
}


Java
// Java program to removes all odd
// frequency elements from an Array
import java.util.*;
 
class GFG {
 
    // Function that removes the
    // elements which have odd
    // frequencies in the array
    static void remove(int arr[], int n) {
        // Create a map to store the
        // frequency of each element
        HashMap mp = new HashMap();
        for (int i = 0; i < n; i++) {
            if (mp.containsKey(arr[i])) {
                mp.put(arr[i], mp.get(arr[i]) + 1);
            } else {
                mp.put(arr[i], 1);
            }
        }
 
        // Remove the elements which
        // have odd frequencies
        for (int i = 0; i < n; i++) {
 
            // If the element has
            // odd frequency then skip
            if ((mp.containsKey(arr[i]) && mp.get(arr[i]) % 2 == 1))
                continue;
 
            System.out.print(arr[i] + ", ");
        }
    }
 
    // Driver code
    public static void main(String[] args) {
        int arr[] = { 3, 3, 3, 2, 2, 4, 7, 7 };
        int n = arr.length;
 
        // Function call
        remove(arr, n);
 
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to removes all odd
# frequency elements from an Array
 
# Function that removes the
# elements which have odd
# frequencies in the array
def remove(arr,  n) :
 
    # Create a map to store the
    # frequency of each element
    m = dict.fromkeys(arr,0);
    for i in range(n) :
        m[arr[i]] += 1;
         
    # Remove the elements which
    # have odd frequencies
    for i in range(n) :
 
        # If the element has
        # odd frequency then skip
        if ((m[arr[i]] & 1)) :
            continue;
             
        print(arr[i],end= ", ");
 
# Driver code
if __name__ == "__main__" :
 
    arr    = [ 3, 3, 3, 2,
            2, 4, 7, 7 ];
    n = len(arr);
 
    # Function call
    remove(arr, n);
 
# This code is contributed by Yash_R


C#
// C# program to removes all odd
// frequency elements from an Array
using System;
using System.Collections.Generic;
 
class GFG {
  
    // Function that removes the
    // elements which have odd
    // frequencies in the array
    static void remove(int []arr, int n) {
 
        // Create a map to store the
        // frequency of each element
        Dictionary mp = new Dictionary();
        for (int i = 0; i < n; i++) {
            if (mp.ContainsKey(arr[i])) {
                mp[arr[i]] =  mp[arr[i]] + 1;
            } else {
                mp.Add(arr[i], 1);
            }
        }
  
        // Remove the elements which
        // have odd frequencies
        for (int i = 0; i < n; i++) {
  
            // If the element has
            // odd frequency then skip
            if ((mp.ContainsKey(arr[i]) && mp[arr[i]] % 2 == 1))
                continue;
  
            Console.Write(arr[i] + ", ");
        }
    }
  
    // Driver code
    public static void Main(String[] args) {
        int []arr = { 3, 3, 3, 2, 2, 4, 7, 7 };
        int n = arr.Length;
  
        // Function call
        remove(arr, n);
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
2, 2, 7, 7,