swift solution using bucket sort func sortZeroesAtEnd(in array: [Int]) -> [Int] { //pseudo bucket sort var zeroBucket: [Int] = [] var nonZeroBucket: [Int] = [] for n in array { if n == 0 { zeroBucket.append(n) } else { nonZeroBucket.append(n) } } return nonZeroBucket + zeroBucket }

var array = [8, 4, 3, 0, 6, 4, 0] var offset = 0 for index in stride(from: array.count - 1, through: 0, by: -1) { if array[index] == 0 { offset += 1 } else { array[(index + offset)] = array[index] if index < offset { array[index] = 0 } } } // result: [0, 0, 8, 4, 3, 6, 4]

func moveZerosToFront(array: [Int]) -> [Int] { var resultArray = [Int]() for num in array { num == 0 ? resultArray.insert(0, at: 0) : resultArray.append(num) } return resultArray }

// in-place solution: func moveZeroesToFront(of array: inout [Int]) { // in-place for i in 0 ..< array.count { if array[i] == 0 { let _ = array.remove(at: i) array.insert(0, at: 0) } } }

A = [8,0,4,3,0,6,4,0,5] offset = len(A)-1 for i in range(len(A)-1,-1,-1): if A[i] != 0: A[offset] = A[i] offset -= 1 for i in range(offset,-1,-1): A[i] = 0 # Returns [0,0,0,8,4,3,6,4,5]

func shiftZeros(inArray ary: [Int]) -> [Int] { var startingPointer: Int = 0 // First position to move 0 to var newArray = ary // Clone the array for iter in 0..

" func shiftZeros(inArray ary: [Int]) -> [Int] { var startingPointer: Int = 0 // First position to move 0 to var newArray = ary // Clone the array for iter in 0..

func shiftZeros(inArray ary: [Int]) -> [Int] { var startingPointer: Int = 0 // First position to move 0 to var newArray = ary // Clone the array for iter in 0..