feat: add swift implementation to lcof problem: No.39 (#2920)

This commit is contained in:
Lanre Adedara 2024-05-27 09:55:25 +01:00 committed by GitHub
parent ebefc762f9
commit 14496937b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 0 deletions

View File

@ -196,6 +196,27 @@ public class Solution {
}
```
#### Swift
```swift
class Solution {
func majorityElement(_ nums: [Int]) -> Int {
var cnt = 0
var m = 0
for v in nums {
if cnt == 0 {
m = v
cnt = 1
} else {
cnt += (m == v ? 1 : -1)
}
}
return m
}
}
```
<!-- tabs:end -->
<!-- solution:end -->

View File

@ -0,0 +1,16 @@
class Solution {
func majorityElement(_ nums: [Int]) -> Int {
var cnt = 0
var m = 0
for v in nums {
if cnt == 0 {
m = v
cnt = 1
} else {
cnt += (m == v ? 1 : -1)
}
}
return m
}
}