Skip to content

Commit

Permalink
Separates the even and odd numbers in an array
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesCreativeContent authored May 10, 2020
1 parent cafbc81 commit e57d9ab
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions separateOddAndEven.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
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.
You may return any answer array that satisfies this condition.
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.
Note:
1 <= A.length <= 5000
0 <= A[i] <= 5000
*/

//Answer//
/**
* @param {number[]} A
* @return {number[]}
*/
var sortArrayByParity = function(A) {
return A.filter(x=>x%2===0).concat(A.filter(x=>x%2!==0))
};

0 comments on commit e57d9ab

Please sign in to comment.