Has anyone worked on getting the most repeated value from an array or in a loop (Lookuprows) via SSJS Script Activity?

For example:

Var Array = [1,2,2,2,2,3,4]

The result should be 2.

I tried passing the SSJS array to a JS variable and getting the result but I would need to do a POST request again to pass it to a SSJS variable.

  • can you please clarify and/or give some use case examples. I am unsure what you mean by 'most repeated value' and 'passing the SSJS array to a JS variable' or 'POST request'. These terms are vaguely connected in your question and with clarity I might be able to better assist. Jan 8, 2020 at 16:30

2 Answers 2


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>

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);


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;
        if(modeMap[el] > maxCount)
            maxEl = el;
            maxCount = modeMap[el];
    return maxEl;
  • This works fine for an array with numbers, but not for string. Sorry, I should have mentioned the string in the question. I have the solution now for strings too, Thanks :) Jan 8, 2020 at 20:29
  • 1
    @AbhiReddyMusku - if you have a solution, could you please share as an answer so that any others that run into this issue can reference your answer? Jan 8, 2020 at 20:57

Thanks @Gortonington for your time and Input. Below is the solution I'm referring to, works with both numbers and strings:

<script runat=server>

var arrAS=["Beds", "Mattresses", "Mattresses", "Beds", "Bedsheets", "Mattresses"]

var x = 1;
var y = 0;
var item;
for (var i=0; i<arrAS.length; i++)
        for (var j=i; j<arrAS.length; j++)
                if (arrAS[i] == arrAS[j])
                if (x<y)
                  item = arrAS[i];
Write(item) ;


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .