LeetCode: Sort Array By Parity Solution
Filter and concatApproach
Filter even elements
Filter odd elements
Concat 2 of them
Implementation
1/**2 * @param {number[]} nums3 * @return {number[]}4 */5var sortArrayByParity = function (nums) {6 const isOdd = x => x % 2 !== 07 const isEven = x => x % 2 === 08 return [...nums.filter(isEven), ...nums.filter(isOdd)]9}
Comments
Loading comments...
Tags
leetcode
array
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Find All Numbers Disappeared in an Array
Hash table to check existing
Previous Post
LeetCode: Replace Elements with Greatest Element on Right Side
Backward iteration, create an array of right max