Skip to content
Related Articles

Related Articles

Count of elements in Array which are present K times & their double isn’t present

Improve Article
Save Article
  • Last Updated : 07 Aug, 2022
Improve Article
Save Article

Given an array arr[] of N integers, the task is to find the count of elements in the array which are present K times and their double are not present in the array. 

Examples:

Input: arr[] = {10, 6, 12, 8, 10, 8}, K = 2
Output: 2
Explanation: 10 is a valid number since it appears exactly two times and 20 does not appear in array.
8 is a valid number since it appears two times and 16 does not appear in array.

Input: arr[] = {1, 3, 5, 3}, K = 3
Output: 0
Explanation: No element in the given array satisfy the condition.

Input: arr[] = {1, 2, 2, 3, 3, 4}, K = 2
Output: 1
Explanation: Only 3 is valid element.
Though 2 is present twice but its double is also present.

 

Approach: The task can be solved using a hashmap Follow the below steps to solve the problem:

  • Store the occurrences of each of the numbers in a hashmap
  • For each element, if the frequency is K, check whether its double is present in the hashmap or not. If it is not present, store it as one of the valid numbers.

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 count valid numbers
int find(int arr[], int N, int K)
{
    // Store ans
    int ans = 0;
    unordered_map<int, int> 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<Integer, Integer> 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<Integer, Integer> 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<int, int> mp =
      new Dictionary<int, int>();
 
    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<int, int> 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




<script>
     // JavaScript code for the above approach
 
     // Function to find the count valid numbers
     function find(arr, N, K)
     {
      
         // Store ans
         let ans = 0;
         let mp = new Map();
         for (let i = 0; i < N; i++) {
 
             // Storing frequency of elements
             if (mp.has(arr[i])) {
                 mp.set(arr[i], mp.get(arr[i]) + 1)
             }
             else {
                 mp.set(arr[i], 1)
             }
         }
 
         for (let [key, val] of mp) {
 
             // Simply checking the
             // given condition in question
             if (val == K) {
                 if (mp.has(key * 2)
                     == false) {
                     ans++;
                 }
             }
         }
         return ans;
     }
 
     // Driver Code
     let arr = [10, 6, 12, 8, 10, 8];
     let N = arr.length;
     let K = 2;
 
     document.write(find(arr, N, K));
      
    // This code is contributed by Potta Lokesh
 </script>


Output

2

Time Complexity: O(N), since one loop is used for traversal of the entire array the algorithm takes up linear time O(N)
Auxiliary Space: O(N), since an unordered map is used in the worst case it takes up all the elements of the array and hence takes up linear space O(N)

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!