Task description:
Implement a function, which receives an array of unsorted numbers from 1 to 100. In the array zero or more numbers might be missing. The function shall return an array containing the missing numbers.
Provided Test-data with expected result:
var arrTest = Array(1...100)
arrTest.remove(at: 25)
arrTest.remove(at: 20)
arrTest.remove(at: 6)
print(findMissingIn(numbers: arrTest))
With these test-data your function shall return [7, 21, 26] as missing numbers.
My solution:
func findMissingIn(numbers: [Int]) -> [Int] {
var missingNums = [Int]()
for i in 1...100 {
if numbers.contains(i) == false {
missingNums.append(i)
}
}
return missingNums
}
My function returns the expected result. So I guess it's formally correct.
- Is there a better solution?
- What's your opinion about my naming and coding-style in general. Would you have done it differently. How? Why?