Filter Primes from an Array

Published by Matt in

Create a function that takes an array and returns a new array containing only prime numbers.

Examples

filter_primes([7, 9, 3, 9, 10, 11, 27]) ➞ [7, 3, 11]

filter_primes([10007, 1009, 1007, 27, 147, 77, 1001, 70]) ➞ [10007, 1009]

filter_primes([1009, 10, 10, 10, 3, 33, 9, 4, 1, 61, 63, 69, 1087, 1091, 1093, 1097]) ➞ [1009, 3, 61, 1087, 1091, 1093, 1097]

Notes

  • New array must maintain the order of primes as they first appear in the original array.
  • Check the Resources tab for help.
Watch a quick demo on how Edabit works.