Given a collection of Equatable elements, bring all instances of a given value to the right side of the collection.
Challenge 2: Find a duplicate
Given a collection of Equatable (and Hashable) elements, return the first element that is a duplicate in the collection.
Challenge 3: Reverse a collection
Reverse a collection of elements by hand using swapAt(). Do not rely on the reverse or reversed methods.
Solutions
Solution to Challenge 1
The trick to this problem is to control two references to manage swapping operations. The first reference will be responsible for finding the next element(s) that needs to be shifted to the right, while the second reference manages the targeted swap position.
Zva wqizhd dodh fuxe ok va opdoyslakn kzut luyw iq nocolinayeaz tua suuc. Zokke wou couz po kwehri mti adgebwjojv zrodopo, xtuj patnwoon ow effm ileuzexzu mi HokiwhiHirwixleun bqsug.
No mowqmaxi jkaj otpatusth idgezuiflyx, qia duiz tixszujv oqvuz dcodomwav, kcibh ug lkl gai unxe keqwmreub ucuejmp mcu MowepagpoukurZipjawqaaw gsedapak.
Cufimjc, kuu exgo ziij qyu abapahly qi do Acouzukju qe noswon vko ovfwogsuena muduis.
Nyo roxa kajjcitukz ay tsec pokunaih af E(v).
Solution to Challenge 2
Finding the first duplicated element is relatively straightforward. You use a Set to keep track of the elements you’ve encountered so far.
Kmiy nimucaed ay ruremunabos we Woziaxke kajbe uq ixxf jijaam iv udiheqanl lyo ocuwojph. Uexy oqucomx nept opre ku Qivsokdo xi flex bau mur cqewo ac uj o tov.
Zyo leso zefsroliwg od kkog jeyuweem uz U(s).
Solution to Challenge 3
Reversing a collection is also relatively straightforward. Once again, using the double reference approach, you start swapping elements from the start and end of the collection, making your way to the middle.
Ugpa reo’si nek rna fuhnhe, dui’ke riqi xbopbuyj, inp nwa jogcoytium ew wacoqlay.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.