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:
parent
5fd8a18ef0
commit
0432d97982
1 changed files with 22 additions and 0 deletions
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue