Majority Element (Easy)
Good evening! Here's our prompt for today.
You may see this problem at Microsoft, Autodesk, Indeed, Pinterest, Atlassian, Tanium, Airtable, Cohesity, Bosch Global, Zoho, and Cornerstone.
Suppose we're given an array of numbers like the following:
[4, 2, 4]
Could you find the majority element? A majority is defined as "the greater part, or more than half, of the total. It is a subset of a set consisting of more than half of the set's elements."
Let's assume that the array length is always at least one, and that there's always a majority element.
In the example above, the majority element would be 4
.

Constraints
- Length of the array <=
100000
- The array will always contain integer values between
-1000000000
and1000000000
- Expected time complexity :
O(n)
- Expected space complexity :
O(n)
xxxxxxxxxx
42
var assert = require('assert');
/**
* @param {number[]} nums
* @return {number}
*/
function majorityElement(nums) {
return nums;
}
try {
assertIsFunction(majorityElement, '`majorityElement` is a function');
console.log('PASSED: ' + '`majorityElement` is a function');
} catch (err) {
console.log(err);
}
try {
assert.equal(majorityElement([1, 4, 2, 4, 4, 3, 4]), 4);
console.log(
'PASSED: ' + '`majorityElement([1, 4, 2, 4, 4, 3, 4])` should return `4`'
);
} catch (err) {
console.log(err);
}
OUTPUT
Results will appear here.