To find the mode (most repeated number inside an array) inside of SFMC SSJS, you can create a simple function, like so:
<script runat=server>
Platform.Load("Core","1.1.1");
var array = [1,2,2,2,2,3,4]
var mode = mode(array)
function mode(arr){
var numMapping = {};
for(var i = 0; i < arr.length; i++){
if(numMapping[arr[i]] === undefined){
numMapping[arr[i]] = 0;
}
numMapping[arr[i]] += 1;
}
var greatestFreq = 0;
var mode;
for(var prop in numMapping){
if(numMapping[prop] > greatestFreq){
greatestFreq = numMapping[prop];
mode = prop;
}
}
return parseInt(mode);
}
</script>
This will then have the SSJS var mode
equal the Mode of the input array.
If you are trying to utilize this against a rowset, like what was returned via a lookup, you will run into a bit of an issue. At that point, it might be better to have a for loop run through that rowset and create an array that you then use to run through the above function.
EDIT - updated to provide function that works on Strings and Numbers:
function mode(array)
{
if(array.length == 0)
return null;
var modeMap = {};
var maxEl = array[0], maxCount = 1;
for(var i = 0; i < array.length; i++)
{
var el = array[i];
if(modeMap[el] == null)
modeMap[el] = 1;
else
modeMap[el]++;
if(modeMap[el] > maxCount)
{
maxEl = el;
maxCount = modeMap[el];
}
}
return maxEl;
}