How to get the first missing smallest positive integer in an unsorted integer list with Swift

1 Answer

0 votes
import Foundation

func findSmallestMissingNumber(_ arr: [Int]) -> Int {
    let numSet = Set(arr) // Convert array to a set for fast lookup

    for index in 1...(arr.max() ?? 0) + 1 {
        if !numSet.contains(index) {
            return index
        }
    }

    return -1 // If no missing number is found
}

let arr = [3, 4, -1, 1]

print(findSmallestMissingNumber(arr))



/*
run:

2

*/

 



answered Jun 5, 2025 by avibootz
...