diff --git a/problems/swift/element-appearing-more-than-25-in-sorted-array.swift b/problems/swift/element-appearing-more-than-25-in-sorted-array.swift new file mode 100644 index 0000000..c8062c4 --- /dev/null +++ b/problems/swift/element-appearing-more-than-25-in-sorted-array.swift @@ -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 + } +}