
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Array elements with prime frequencies in C++?
Array is a container of elements of same data type.
Prime Frequencies means that the number of occurrence of the element of the array is a prime number.
So, based on these definitions the problem to find array elements with prime frequencies. We are given a string of array. We have to find the frequency of characters and check if frequency is prime and then count elements that have prime frequencies.
Let’s take an example,
Input: str = “helloworld” Output: 2
Explanation
Count of occurrences of characters are −
h -> 1 e -> 1 l -> 3 o -> 2 w-> 1 r -> 1 d -> 1
l occurs prime number of times i.e. 3 and o occurs prime number of times i.e. 2.
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
To check for prime frequency of elements we will check for the occurrence of elements of and check if the count is prime or not. For this we will take maps under consideration.
Example
#include <iostream> #include <bits/stdc++.h> using namespace std; int check_prime(int n) { if (n <= 1) return 0; if (n <= 3) return 1; if (n % 2 == 0 || n % 3 == 0) return 0; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return 0; return 1; } int countPrimeFrequent(string s) { int count = 0; unordered_map<char, int> mp; for (int i = 0; i < s.length(); i++) mp[s[i]]++; for (auto it = mp.begin(); it != mp.end(); it++) { if (check_prime(it->second)) count++; } return count; } int main() { string s = "helloworld"; cout << countPrimeFrequent(s); return 0; }
Output
The repeat elements of the array are : 2
- Related Articles
- Array elements with prime frequencies?
- Counting frequencies of array elements in C++
- Prime factors of LCM of array elements in C++
- Count frequencies of all elements in array in Python\n
- JavaScript program for counting frequencies of array elements
- XOR of Prime Frequencies of Characters in a String in C++
- Count frequencies of all elements in array in Python using collections module
- Javascript Program for Range Queries for Frequencies of array elements
- Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
- Print prime numbers with prime sum of digits in an array
- Check whether the sum of prime elements of the array is prime or not in Python
- Prime numbers after prime P with sum S in C++
- How to replace elements in array with elements of another array in JavaScript?
- Count subarrays with Prime sum in C++
- Largest number with prime digits in C++
