-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmajority-element.js
72 lines (66 loc) · 1.92 KB
/
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
68
69
70
71
72
/**
* Source: https://leetcode.com/problems/majority-element/
* Tags: [Divide and Conquer,Array,Bit Manipulation]
* Level: Easy
* Updated: 2015-04-24
* Title: Majority Element
* Auther: @imcoddy
* Content: 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.
*
* Credits:Special thanks to @ts for adding this problem and creating all test cases.
*/
/**
* @param {number[]} num
* @return {number}
*/
/**
* Memo: Use a map to record how many thime this element appeared, then find the one passed [n/2] times
* Complex: O(n)
* Runtime: 136ms
* Tests: 40 test cases passed
* Rank: A
* Updated: 2015-06-12
*/
var majorityElement = function(num) {
var map = {};
for (var i = 0; i < num.length; i++) {
if (map[num[i]]) {
map[num[i]] += 1;
if (map[num[i]] >= (num.length / 2)) {
return parseInt(num[i], 10);
}
} else {
map[num[i]] = 1;
}
}
var result = 0;
var max = 0;
for (var k in map) {
if (map[k] > max) {
result = k;
max = map[k];
}
}
return parseInt(result, 10);
};
/**
* Memo: Sort the array first, then the majority element will be located at n/2.
* Complex: O(nlogn)
* Runtime: 148ms
* Tests: 40 test cases passed
* Rank: A
* Updated: 2015-06-12
*/
var majorityElement = function(num) {
return num.sort()[num.length >> 1];
};
var should = require('should');
console.time('Runtime');
majorityElement([1]).should.equal(1);
//majorityElement([1, 2]).should.equal(1);
//majorityElement([2, 1]).should.equal(1); // this is a bit tricky as it could be either 1 or 2
majorityElement([2, 1, 2]).should.equal(2);
majorityElement([1, 8, 3, 3, 3, 5]).should.equal(3);
console.timeEnd('Runtime');