Sort Array By Parity

Easy

Return an array with all the even integers of nums first, followed by all the odd integers.

The relative order within each half must be preserved.

Example 1

sortArrayByParity()
Input
nums = [3,1,2,4]
Output
[2,4,3,1]

Why

Evens `[2,4]` then odds `[3,1]`.

Example 2

sortArrayByParity()
Input
nums = [0]
Output
[0]
Constraints
1 <= nums.length <= 5000
0 <= nums[i] <= 5000

Topics

ArraysTwo PointersSorting

Implement sortArrayByParity in the editor and return the answer — printing it is never graded.