File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ Code
2
+ ====
3
+
4
+ ``` go
5
+ func majorityElement (nums []int ) int {
6
+ n := int (len (nums) / 2 )
7
+
8
+ count := make (map [int ]int , 0 )
9
+
10
+ for _ , num := range nums {
11
+ if _ , ok := count[num]; ok {
12
+ count[num] += 1
13
+ } else {
14
+ count[num] = 1
15
+ }
16
+ }
17
+
18
+ for num , c := range count {
19
+ if c > n {
20
+ return num
21
+ }
22
+ }
23
+ return 0
24
+ }
25
+ ```
26
+
27
+ Solution in mind
28
+ ================
29
+
30
+ - Iterate through given array and keep track of count of numbers in a map (number: count).
31
+
32
+ - Iterate through the map and return the first number which has a number greater than floor(n/2).
Original file line number Diff line number Diff line change 37
37
38
38
- [ symmetric-tree] ( Easy/symmetric-tree.md )
39
39
40
+ - [ majority-element] ( Easy/majority-element.md )
41
+
40
42
Medium
41
43
------
42
44
You can’t perform that action at this time.
0 commit comments