importFoundation/*
6-10PM Challenge part 1
@param mixedArray is a ordered list of Ints with zeros mixed in randomly.
@return the same list of Ints with order preserved, except that all the zeros have been moved to the end.
*/funczerosToEnd(mixedArray:[Int])->[Int]{returnmixedArray.filter({$0!=0})+mixedArray.filter({$0==0})}letexample1=[12,3,0,2,8,11,0,0,6,4,0,5,7,0,8,9,0]letsolution1=[12,3,2,8,11,6,4,5,7,8,9,0,0,0,0,0,0]print(zerosToEnd(mixedArray:example1),"\n")print("Testing example1 matches solution1: ",(zerosToEnd(mixedArray:example1)==solution1),"\n\n")
Swift solution:
Output:
I opted to just filter out all the zeros entirely/filter everything but zeros, then recombine in the correct order.