Given an array of integers, where all elements but one occur twice, find the unique element.
Example
a = [1, 2, 4, 3, 3, 2, 1]
The unique element is 4.
func lonelyinteger(a: [Int]) -> Int {
var dics = [Int : Int]()
for i in a {
if dics[i] == nil {
dics[i] = 1
} else {
dics[i]! += 1
}
}
return dics.first{ $0.value == 1 }!.key
}
'Computer Science > Algorithm with Code' 카테고리의 다른 글
Leet Code (2405. Amazon Spring '23 HF) (0) | 2024.05.13 |
---|---|
Leet Code (1492. Amazon Spring '23 HF) (0) | 2024.05.13 |
Hacker Rank_4/54 tests (Easy, Sparse Array) (0) | 2024.05.09 |
Hacker Rank_3/54 tests (Easy, Time Conversion) (0) | 2024.05.08 |
Hacker Rank_2/54 tests (Easy, Min-Max Sum) (0) | 2024.05.07 |