// A majority element = element that appears more than size/2 times in an array[size]
class MyClass
{
static int getMajorityElement(List<int> lst) {
List<int> num = List.generate(10,(i_0)=>0,growable: false);
var len = lst.length;
for (var i = 0; i < len; i++) {
num[lst[i]]++;
}
for (var i = 0; i < 9; i++) {
if (num[i] != 0) {
var val = num[i];
print("$i : $val");
if (num[i] > len / 2) {
return i;
}
}
}
return 0;
}
static void main()
{
List<int> lst = [2, 5, 3, 5, 5, 1, 5, 5, 5, 7, 3];
var majority = MyClass.getMajorityElement(lst);
if (majority != 0) {
print("majority element = $majority");
}
else {
print("Majority element doesn\'t exists\n");
}
}
}
void main() {
MyClass.main();
}
/*
run:
1 : 1
2 : 1
3 : 2
5 : 6
majority element = 5
*/