code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

905 Sort Array By Parity

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
 * Given an array A of non-negative integers, return an array consisting of
 * all the even elements of A, followed by all the odd elements of A.
 * 
 * Example 1:
 * Input: [3,1,2,4]
 * Output: [2,4,3,1]
 * The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
 */

function sortArrayByParity(nums) {
  return [].concat(nums.filter(n => n % 2 === 0), nums.filter(n => n % 2 !== 0))
}

test('sort Array By Parity', () => {
  expect(sortArrayByParity([3,1,2,4])).toEqual([2,4,3,1])
});

Created 2020-04-21T10:37:54+00:00 · Edit