-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path169__easy__majority-element.js
67 lines (56 loc) · 1.2 KB
/
169__easy__majority-element.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
*
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
Example 1:
Input: [3,2,3]
Output: 3
Example 2:
Input: [2,2,1,1,1,2,2]
Output: 2
*/
/**
* Because we know majority element is more than 50%, it will always win the count
* O(n) time
* O(1) space
*
* 10m
*
* Runtime: 60 ms, faster than 98.78%
* Memory Usage: 37.1 MB, less than 97.09%
*
*/
var majorityElement = function(nums) {
let count = 1;
let curr_num = nums[0];
for (let i = 1; i < nums.length; i++) {
if (curr_num == nums[i]) count++;
else count--;
if (count === 0) {
count = 1;
curr_num = nums[i];
}
}
return curr_num;
};
/**
* Use hashmap, this is slower than 1st one and using O(n) space
*
* O(n) time
* O(n) space
*
* 10m
*/
var majorityElement = function(nums) {
let map = {};
let n = nums.length;
let half = ~~(n/2);
for (let i = 0; i < n; i++) {
let num = nums[i];
map[num] = (map[num] || 0) + 1;
if (map[num] > half ) {
return num;
}
}
return false;
};