Even Number Filter
easySave
Array
Given an array of integers, write a function that returns a new array containing only the even numbers from the original array.
Example 1
Input
[ 1, 2, 3, 4, 5 ]
Output
[2, 4]
Explanation. Only 2 and 4 are even numbers from the input array [1, 2, 3, 4, 5].
Example 2
Input
[ -2, -3, -4, 0, 10 ]
Output
[-2, -4, 0, 10]
Explanation. The even numbers in this array are -2, -4, 0, and 10.
Example 3
Input
[ 7, 13, 19, 23 ]
Output
[]
Explanation. There are no even numbers in the array [7, 13, 19, 23].
Follow-up: Can you solve this problem using a higher-order function (like `filter` in JavaScript or Python)? What would be the time complexity of your solution?
Constraints:
The input array may contain both positive and negative integers, including zero. The array will contain at least one integer.
- Views
- 3