Array elements with prime frequencies
Given a string. The task is to find the count of characters whose number of occurrences is prime.
Examples:
Input : str = "geeksforgeeks" Output : 3 Count of occurrences of characters are: g -> 2 e -> 4 k -> 2 s -> 2 f -> 1 o -> 1 r -> 1 So, g, k and s occurs prime number of times i.e. 2 Input : str = "aabbcc" Output : 3
Start traversing the string and count the occurrences of each character using a map in C++ and check whether an occurrence is prime or not. If prime then increment the count otherwise not.
Below is the implementation of the above approach:
C++
// C++ program to find count of numbers // with prime frequencies #include <bits/stdc++.h> using namespace std; // Function to check if a // number is prime bool check_prime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to find count of numbers // with prime frequencies int countPrimeFrequent(string s) { int count = 0; // create a map to store // frequency of each character unordered_map<char, int> mp; // Store frequncy of each character // in the map for (int i = 0; i < s.length(); i++) mp[s[i]]++; // now iterate the map and characters // with prime occurrences for (auto it = mp.begin(); it != mp.end(); it++) { // if prime then increment count if (check_prime(it->second)) count++; } return count; } // Driver Code int main() { string s = "geeksforgeeks"; cout << countPrimeFrequent(s); return 0; } |
chevron_right
filter_none
Java
// Java program to find count of numbers // with prime frequencies import java.util.*; class GFG { // Function to check if a // number is prime static boolean check_prime(int n) { // Corner cases if (n <= 1) { return false; } if (n <= 3) { return true; } // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) { return false; } for (int i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } // Function to find count of numbers // with prime frequencies static int countPrimeFrequent(String s) { int count = 0; // create a map to store // frequency of each character Map<Character, Integer> mp = new HashMap<>(); // Store frequncy of each character // in the map for (int i = 0; i < s.length(); i++) { if (mp.containsKey(s.charAt(i))) { mp.put(s.charAt(i), mp.get(s.charAt(i)) + 1); } else { mp.put(s.charAt(i), 1); } } // now iterate the map and characters // with prime occurrences for (Map.Entry<Character, Integer> entry : mp.entrySet()) { // if prime then increment count if (check_prime(entry.getValue())) { count++; } } return count; } // Driver Code public static void main(String[] args) { String s = "geeksforgeeks"; System.out.println(countPrimeFrequent(s)); } } // This code has been contributed by 29AjayKumar |
chevron_right
filter_none
Python3
# python3 program to find count of numbers # with prime frequencies # Function to check if a # number is prime def check_prime(n): # Corner cases if (n <= 1): return False if (n <= 3): return True # This is checked so that we can skip # middle five numbers in below loop if (n % 2 == 0 or n % 3 == 0): return False for i in range(5,n+1,6): if (n % i == 0 or n % (i + 2) == 0): return False return True # Function to find count of numbers # with prime frequencies def countPrimeFrequent(s): count = 0 # create a map to store # frequency of each character mp={} # Store frequncy of each character # in the mp for i in range(0,len(s)): mp.setdefault(s[i],0) mp[s[i]]+=1 # now iterate the map and characters # with prime occurrences for i in mp.keys(): # if prime then increment count if (check_prime(mp[i])): count+=1 return count; # Driver Code s = "geeksforgeeks" print(countPrimeFrequent(s)) # This code is improved by sahilshelangia |
chevron_right
filter_none
C#
// C# program to find count of numbers // with prime frequencies using System; using System.Collections.Generic; class GFG { // Function to check if a // number is prime static Boolean check_prime(int n) { // Corner cases if (n <= 1) { return false; } if (n <= 3) { return true; } // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) { return false; } for (int i = 5; i * i <= n; i = i + 6) { if (n % i == 0 || n % (i + 2) == 0) { return false; } } return true; } // Function to find count of numbers // with prime frequencies static int countPrimeFrequent(String s) { int count = 0; // create a map to store // frequency of each character Dictionary<char, int> mp = new Dictionary<char,int>(); // Store frequncy of each character // in the map for (int i = 0; i < s.Length; i++) { if (mp.ContainsKey(s[i])) { var v = mp[s[i]] + 1; mp.Remove(s[i]); mp.Add(s[i], v); } else { mp.Add(s[i], 1); } } // now iterate the map and characters // with prime occurrences foreach(KeyValuePair<char, int> entry in mp) { // if prime then increment count if (check_prime(entry.Value)) { count++; } } return count; } // Driver Code public static void Main(String[] args) { String s = "geeksforgeeks"; Console.WriteLine(countPrimeFrequent(s)); } } // This code is contributed by Rajput-Ji |
chevron_right
filter_none
Output:
3
Recommended Posts:
- Elements of first array that have more frequencies
- Counting frequencies of array elements
- Find array elements with frequencies in range [l , r]
- Range Queries for Frequencies of array elements
- Sum of elements in an array with frequencies greater than or equal to that element
- Count frequencies of all elements in array in O(1) extra space and O(n) time
- Count frequencies of all elements in array in Python using collections module
- Check whether the sum of prime elements of the array is prime or not
- XOR of Prime Frequencies of Characters in a String
- Sum and Product of Prime Frequencies of Characters in a String
- Numbers with prime frequencies greater than or equal to k
- Check whether the frequencies of all the characters in a string are prime or not
- Print characters having prime frequencies in order of occurrence
- XOR of elements in an array having prime frequency
- Find the sum of non-prime elements in the given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



