📌  相关文章
📜  Array 中存在 K 次且不存在双精度的元素的数量

📅  最后修改于: 2022-05-13 01:56:10.147000             🧑  作者: Mango

Array 中存在 K 次且不存在双精度的元素的计数

给定一个包含 N 个整数的数组arr[] ,任务是找出数组中存在K 次且它们的 double 不存在于数组中的元素的计数。

例子:

方法:可以使用hashmap解决任务按照以下步骤解决问题:

  • 将每个数字的出现存储在哈希图中
  • 对于每个元素,如果频率K ,请检查其精度值是否存在于哈希图中。如果不存在,请将其存储为有效数字之一。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the count valid numbers
int find(int arr[], int N, int K)
{
    // Store ans
    int ans = 0;
    unordered_map mp;
    for (int i = 0; i < N; i++) {
 
        // Storing frequency of elements
        mp[arr[i]]++;
    }
 
    for (auto it : mp) {
        // Simply checking the
        // given condition in question
        if (it.second == K) {
            if (mp.find(it.first * 2)
                == mp.end()) {
                ans++;
            }
        }
    }
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 6, 12, 8, 10, 8 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 2;
    cout << find(arr, N, K);
    return 0;
}


Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
 
  // Function to find the count valid numbers
  public static int find(int[] arr, int N, int K)
  {
 
    // Store ans
    int ans = 0;
    HashMap mp = new HashMap<>();
 
    for (int i = 0; i < N; i++) {
 
      // Storing frequency of elements
      if (mp.containsKey(arr[i])) {
        mp.put(arr[i], mp.get(arr[i]) + 1);
      }
      else {
        mp.put(arr[i], 1);
      }
    }
 
    for (Map.Entry i :
         mp.entrySet()) {
 
      // Simply checking the
      // given condition in question
      if (i.getValue() == K) {
        if (mp.containsKey(i.getKey() * 2)
            == false) {
          ans++;
        }
      }
    }
    return ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = new int[] { 10, 6, 12, 8, 10, 8 };
    int N = arr.length;
    int K = 2;
    System.out.print(find(arr, N, K));
  }
}
 
// This code is contributed by Taranpreet


Python3
# Python program for the above approach
 
# Function to find the count valid numbers
def find(arr, N, K):
    # Store ans
    ans = 0
    mp = {}
    for i in range(N):
        # Storing frequency of elements
        if arr[i] not in mp:
          mp[arr[i]] = 1
        else:
          mp[arr[i]]+=1
     
    for i in mp:
        # Simply checking the
        # given condition in question
        if (mp[i] == K):
            if ((i * 2) not in mp):
                ans += 1
    return ans
 
# Driver Code
arr = [ 10, 6, 12, 8, 10, 8 ]
N = len(arr)
K = 2
print(find(arr, N, K))
 
# This code is contributed by rohitsingh07052


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to find the count valid numbers
  public static int find(int[] arr, int N, int K)
  {
 
    // Store ans
    int ans = 0;
    Dictionary mp =
      new Dictionary();
 
    for (int i = 0; i < N; i++) {
 
      // Storing frequency of elements
      if (!mp.ContainsKey(arr[i])) {
        mp.Add(arr[i], 1);
      }
      else {
        mp[arr[i]] = mp[arr[i]] + 1;
      }
    }
 
    foreach(KeyValuePair x in mp)
    {
 
      // Simply checking the
      // given condition in question
      if (x.Value == K) {
        if (mp.ContainsKey(x.Key * 2) == false) {
          ans++;
        }
      }
    }
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = new int[] { 10, 6, 12, 8, 10, 8 };
    int N = arr.Length;
    int K = 2;
    Console.Write(find(arr, N, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
2

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