Swift coding challenge!!
First Duplicate. Given an array a that contains only numbers in the range from 1 to a.length, find the first duplicate number for which the second occurrence has the minimal index.
I have included a solutions video!! The video gives some insight into how Iβve solved the problem. Donβt forget to try the problem out BEFORE checking out the solution video and solution code π π
If you have a coding challenge you want me to solve feel free to drop a comment or message me with the problem!! π π letβs get swole πͺπΎ π» π»
Top comments (1)
let array = [2, 1, 3, 5, 3, 2]
func firstDuplicateIn (array: [Int]) -> Int {
var array2 = Int
for i in 0..<array.count {
if array2.contains(array[i]) {
return array[i]
} else {
array2.append(array[i])
}
}
return -1
}
print(firstDuplicateIn(array: array))