forked from msvz/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajority_element.js
More file actions
77 lines (64 loc) · 1.8 KB
/
majority_element.js
File metadata and controls
77 lines (64 loc) · 1.8 KB
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
73
74
75
76
77
/*
**************************************************************************************************************
Majority Element: A majority element in an array A[] of size n is an element that appears more than n/2 times (and hence there is at most one such element).
Write a function which takes an array and emits the majority element (if it exists), otherwise prints NONE as follows:
I/P : 3 3 4 2 4 4 2 4 4
O/P : 4
I/P : 3 3 4 2 4 4 2 4
O/P : NONE
**************************************************************************************************************
var arr = [3, 3, 4, 2, 4, 4, 2, 4, 4];
arr.sort() -> [2, 2, 3, 3, 4, 4, 4, 4, 4]
*/
function majorityElement(arr) {
var hash = {}
arr.forEach(function(item) {
if (hash.hasOwnProperty(item)) {
hash[item] = hash[item] + 1;
} else {
hash[item] = 1;
}
});
var n = arr.length,
majority = "NONE";
Object.keys(arr).forEach(function(key) {
if (hash[key] > n/2) {
majority = key;
}
});
return majority;
}
console.log(majorityElement([3, 3, 4, 2, 4, 4, 2, 4, 4]));
console.log(majorityElement([3, 3, 4, 2, 4, 4, 2, 4]));
/*
Moore's voting algorithm works only if majority elem is defined as > n/2.
Initialize:
majority_elem = arr[0];
count = 0;
*/
function votingalgoithm(arr) {
var majority_elem = arr[0],
count = 0,
n = arr.length;
for(var i=1; i < n; i++) {
if(arr[i] === majority_elem) {
count++;
} else {
if (count !== 0) {
count--;
} else {
majority_elem = arr[i];
}
}
}
count = 0;
arr.forEach(function(elem) {
if (elem == majority_elem) {
count ++;
}
});
if (count > n/2) return majority_elem;
else return "NONE";
}
console.log(votingalgoithm([3, 3, 4, 2, 4, 4, 2, 4, 4]));
console.log(votingalgoithm([3, 3, 4, 2, 4, 4, 2, 4]));