풀이1 : Set의 활용
public func solution(_ X : Int, _ A : inout [Int]) -> Int {
var leaves = Set<Int>()
for i in 0..<A.count {
leaves.insert(A[i])
if leaves.count == X {
return i
}
}
return -1
}
풀이2 : Frequency Map의 활용
public func solution(_ X : Int, _ A : inout [Int]) -> Int {
var frequencyMap: [Int: Bool] = [:]
for i in 1...X {
frequencyMap[i] = false
}
for i in 0..<A.count {
frequencyMap[A[i]] = true
guard !frequencyMap.values.contains(false) else {
continue
}
return i
}
return -1
}