I'm using this algorithm to find the most common string in an array the only problem is when an array contains two elements or equals amounts of strings the following method does not work exp: (arr["Test","Test2"]
) returns nothing
Arr["Test", "Test", "Test","Test2"]
//Should return "Test"
_correctName(array) {
const modeMap = {};
let maxElement = "";
let maxCount = 1;
for (let i = 0; i < array.length; i++) {
const element = array[i];
if (modeMap[element] == null)
modeMap[element] = 1;
else
modeMap[element]++;
if (modeMap[element] > maxCount) {
maxElement = element;
maxCount = modeMap[element];
}
}
return maxElement;
};