-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
๐ฌย ๋ฌธ์
https://app.codility.com/programmers/trainings/4/first_unique/
๐ฌย Idea
- ์์ ํ์์ ํตํด Dictionary์ ํด๋น key๊ฐ์ด ์ฒ์ ๋ํ๋ฌ์ ๋์ index์ key๊ฐ์ด ๋ช๋ฒ ๋ํ๋ฌ๋์ง๋ฅผ ์ ์ฅํ๋ค.
- ์ดํ ๊ฐ์ด ๊ณ ์ ํ์ง ์์ ๊ฒ๋ค์ ๋ชจ๋ ํํฐ๋งํ ๋ค index๋ฅผ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ์ฌ ์ฒซ key๋ฅผ ๋ฆฌํดํ๋ค. ์ด ๋ ๋น ๋ฐฐ์ด์ด๋ผ๋ฉด -1์ ๋ฆฌํดํ๋ค.
๐ฌย ํ์ด
public func solution(_ A : inout [Int]) -> Int {
var dict: [Int: (Int, Int)] = [:]
if A.count == 1 { return A.first! }
if A.count == 0 { return -1 }
for (index, i) in A.enumerated() {
if dict[i] == nil {
dict[i] = (index, 1)
} else {
dict[i]!.1 += 1
}
}
dict = dict.filter({ $0.value.1 == 1 })
return dict.sorted(by: { $0.value.0 < $1.value.0 }).first?.key ?? -1
}
์์์๊ฐ
: 8๋ถ
์๊ฐ ๋ณต์ก๋
: O(N * log(N))
ํ๊ฐํ
: https://app.codility.com/demo/results/trainingV9V5FJ-6NK/