Skip to content

Commit d3dcd82

Browse files
committed
majority_element
1 parent 8a573b4 commit d3dcd82

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
146146
#### [166. Fraction to Recurring Decimal](https://github.com/hitzzc/go-leetcode/tree/master/fraction_to_recurring_decimal)
147147
#### [167. Two Sum II](https://github.com/hitzzc/go-leetcode/tree/master/two_sum_II)
148148
#### [168. Excel Sheet Column Title](https://github.com/hitzzc/go-leetcode/tree/master/excel_sheet_column_title)
149+
#### [169. Majority Element](https://github.com/hitzzc/go-leetcode/tree/master/majority_element)
149150

150151

151152

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package majority_element
2+
3+
func majorityElement(nums []int) int {
4+
var elem, count int
5+
for i := range nums {
6+
if count == 0 {
7+
count++
8+
elem = nums[i]
9+
} else {
10+
if nums[i] != elem {
11+
count--
12+
} else {
13+
count++
14+
}
15+
}
16+
}
17+
return elem
18+
}

0 commit comments

Comments
 (0)