VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-number-of-primes-in-an-array/

⇱ Count number of primes in an array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count number of primes in an array

Last Updated : 11 Jul, 2025

Given an array arr[] of N positive integers. The task is to write a program to count the number of prime elements in the given array.

Examples

Input: arr[] = {1, 3, 4, 5, 7}
Output: 3
There are three primes, 3, 5 and 7

Input: arr[] = {1, 2, 3, 4, 5, 6, 7}
Output: 4

Naive Approach: A simple solution is to traverse the array and keep checking for every element if it is prime or not and keep the count of the prime elements at the same time.

Efficient Approach: Generate all primes upto maximum element of the array using sieve of Eratosthenes and store them in a hash. Now traverse the array and find the count of those elements which are prime using the hash table. 

Below is the implementation of above approach: 


Output
4
Comment
Article Tags: