Array elements with prime frequencies
Last Updated :
03 Mar, 2023
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.
Algorithm:
- Create a static function named "check_prime" with a boolean return type that takes an integer as the input value.
- that will return true or false if the number is prime or not.
- Create a static function named countPrimeFreuent with in return type which takes a string as input.
- initialize an int variable count with a value of 0
- To store the frequency of each character in the string s, create a hashmap called mp.
- start for loop inside function which traverses the input string and updates its frequency in mp
- now iterate the map and characters with prime occurrences, If the frequency is prime, increment the count variable
- return the value of the count
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 frequency 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;
}
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 frequency 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
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 frequency 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
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 frequency 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
JavaScript
<script>
// Javascript program to find count of numbers
// with prime frequencies
// Function to check if a
// number is prime
function 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 || n % 3 == 0)
return false;
for (let 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
function countPrimeFrequent(s) {
let count = 0;
// create a map to store
// frequency of each character
let mp = new Map();
// Store frequency of each character
// in the map
for (let i = 0; i < s.length; i++) {
if (mp.has(s[i])) {
mp.set(s[i], mp.get(s[i]) + 1);
}
else {
mp.set(s[i], 1);
}
}
// now iterate the map and characters
// with prime occurrences
for (let it of mp) {
// if prime then increment count
if (check_prime(it[1]))
count++;
}
return count;
}
// Driver Code
let s = "geeksforgeeks";
document.write(countPrimeFrequent(s));
// This code is contributed by Saurabh Jaiswal
</script>
Time Complexity: O(n + n * sqrt(n)). where n is the length of the string.
Auxiliary Space: O(m). where m is the number of unique characters in the input string.
Similar Reads
Find array elements with frequencies in range [l , r] Given an array of integers, find the elements from the array whose frequency lies in the range [l, r]. Examples: Input : arr[] = { 1, 2, 3, 3, 2, 2, 5 } l = 2, r = 3 Output : 2 3 3 2 2 Approach : Take a hash map, which will store the frequency of all the elements in the array.Now, traverse once agai
9 min read
Mode of frequencies of given array elements Given an array arr[], the task is to find the mode of frequencies of elements of the given array. Examples: Input: arr[] = {6, 10, 3, 10, 8, 3, 6, 4}, N = 8Output: 2Explanation:Here (3, 10 and 6) have frequency 2, while (4 and 8) have frequency 1.Three numbers have frequency 2, while 2 numbers have
6 min read
Range Queries for Frequencies of array elements Given an array of n non-negative integers. The task is to find frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples: Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; lef
13 min read
Sum of elements in an array having prime frequency Given an array arr, the task is to find the sum of the elements which have prime frequencies in the array. Note: 1 is neither prime nor composite.Examples: Input: arr[] = {5, 4, 6, 5, 4, 6} Output: 15 All the elements appear 2 times which is a prime So, 5 + 4 + 6 = 15Input: arr[] = {1, 2, 3, 3, 2, 3
7 min read
XOR of elements in an array having prime frequency Given an array arr[] of N elements, the task is to find the xor of the elements which have prime frequencies in the array. Note that 1 is neither prime nor composite. Examples: Input: arr[] = {5, 4, 6, 5, 4, 6} Output: 7 Explanation: All the elements appear 2 times which is a prime So, 5 ^ 4 ^ 6 = 7
7 min read