544.Keep Only the Prime Numbers
Easy
Given an array
nums, return a new array containing only the values that are prime, in their original order.
Checking a single number for primality is its own exercise; here the point is decompositionDecompositionSplitting a problem into smaller named pieces — e.g. "is this prime?" and "keep the ones that pass".. Pulling the primality test out into isPrime(n) leaves the main function with one simple job, and gives the test a place where it is easy to improve: the helper stops at √n, while the inline version checks every candidate up to n − 1.
Example 1:
Input: nums = [10,2,9,13,1]
Output: [2,13]
Example 2:
Input: nums = [4,6,8]
Output: []
+ 5 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 30 - ●
0 ≤ nums[i] ≤ 10000
nums =
[10, 2, 9, 13, 1]