This is an open source platform.
Create coding videos, upload them on YouTube, and help the community grow.
Teaching is the fastest way to learn β and the best way to never forget
what you know π
β‘ Start Recording
Screen only
Screen With Camera
β‘Start Recording
β
Selected Template
${temp1}
Screen only
Screen With Camera
Start Recording
β
β‘ Show Preview
Thumbnail Templates
Generating Thumbnail...
Master In-Demand Tech. Get Paid
Sort Array of 0s and 1s
Given an array consisting only of 0s and 1s, sort the array.
Difficulty:
JavaScript Solution
function sortZeroOne(nums) {
let count0 = 0;
for (let num of nums) if (num === 0) count0++;
for (let i = 0; i < nums.length; i++) {
nums[i] = i < count0 ? 0 : 1;
}
return nums;
}
Explanation: We count the number of zeros and overwrite the array accordingly.