

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Array elements with prime frequencies?
Suppose we have one array. we have to count how many of the elements present in the array prime number of times. So if the array is {1, 2, 2, 0, 1, 5, 2, 5, 0, 0, 1, 1}, then 1 is present 4 times, 2 is present 3 times, 0 is present 3 times, and 5 is present 2 times. So there are three elements {2, 0, 5} that have occurred prime number of times. So the count will be 3.
Algorithm
countPrimeOccurrence(arr, n)
Begin count := 0 define map with int type key and int type value for each element e in arr, do increase map.key(arr).value done for each key check whether the value corresponding the value is prime or not, if prime, then increase count. return count End
Example
#include <iostream> #include <map> using namespace std; bool isPrime(int n){ for(int i = 2; i<=n/2; i++){ if(n % i == 0){ return false; } } return true; } int countPrimeOcurrence(int arr[], int n){ int count = 0; map<int, int> freq_map; for(int i = 0; i<n; i++){ freq_map[arr[i]]++; //increase the frequency } for (auto it = freq_map.begin(); it != freq_map.end(); it++) { if (isPrime(it->second)) count++; } return count; } int main() { int arr[] = {1, 2, 2, 0, 1, 5, 2, 5, 0, 0, 1, 1}; int n = sizeof(arr)/sizeof(arr[0]); cout << "Prime frequency count: " << countPrimeOcurrence(arr, n); }
Output
Prime frequency count: 3
- Related Questions & Answers
- Array elements with prime frequencies in C++?
- Counting frequencies of array elements in C++
- Count frequencies of all elements in array in Python
- Count frequencies of all elements in array in Python using collections module
- XOR of Prime Frequencies of Characters in a String in C++
- Prime factors of LCM of array elements 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
- Querying array elements with MongoDB?
- Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
- Program to check whether elements frequencies are even or not in Python
- Print numbers in descending order along with their frequencies
- How to replace elements in array with elements of another array in JavaScript?
- Check whether the frequencies of all the characters in a string are prime or not in Python
- How to find the table of ordered frequencies of vector elements in R?
Advertisements