Sort a Binary Array
Problem statement
Given an integer array containing only 0 and 1, return the values in non-decreasing order.
Preserve the input array and return a newly arranged array.
Function
sortBinaryArray(nums: int[]) → int[]Examples
Example 1
nums = [1,0,1,0,0]return = [0,0,0,1,1]All zeros precede all ones.
Example 2
nums = [1,1]return = [1,1]An already sorted array is unchanged.
Constraints
0 <= nums.length <= 100000.- Every value is
0or1.
Source note: Author-written prompt excerpt retained for source fidelity.