problems(swift): add “1287. Element Appearing More Than 25% In Sorted Array”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-11 18:10:27 +01:00
parent 5fd8a18ef0
commit 0432d97982
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,22 @@
class Solution {
func findSpecialInteger(_ arr: [Int]) -> Int {
let threshold = arr.count / 4
var last = -1
var counter = 0
for x in arr {
if last != x {
counter = 1
last = x
} else {
counter += 1
}
if counter > threshold {
return last
}
}
return -1
}
}