CodeWars: Remove Zeroes Solution
1function removeZeros(array) {2 // Sort "array" so that all elements with the value of zero are moved to the3 // end of the array, while the other elements maintain order.4 // [0, 1, 2, 0, 3] --> [1, 2, 3, 0, 0]5 // Zero elements also maintain order in which they occurred.6 // [0, "0", 1, 2, 3] --> [1, 2, 3, 0, "0"]78 // Do not use any temporary arrays or objects. Additionally, you're not able9 // to use any Array or Object prototype methods such as .shift(), .push(), etc1011 // the correctly sorted array should be returned.12 let N = array.length13 for (let i = 0; i < N; i++) {14 let times = N - i15 while ((array[i] === 0 || array[i] === "0") && times--) {16 for (let j = i + 1; j < N; j++) {17 ;[array[j], array[j - 1]] = [array[j - 1], array[j]]18 }19 }20 }21 return array22}
Comments
Loading comments...
Tags
codewars
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.