This is a great function that you can use to eliminate duplicates from an array in Service-now. It’s just standard JavaScript so it will work in Client Scripts, Business Rules, or pretty much anywhere else you want to put it. The function takes an array as the input parameter and returns an array…minus the duplicate values!

function checkDuplicates(a) {
   //Check all values in the incoming array and eliminate any duplicates
   var r = new Array(); //Create a new array to be returned with unique values
   //Iterate through all values in the array passed to this function
   o:for(var i = 0, n = a.length; i < n; i++){
      //Iterate through any values in the array to be returned
      for(var x = 0, y = r.length; x < y; x++){
         //Compare the current value in the return array with the current value in the incoming array
         if(r[x]==a[i]){
            //If they match, then the incoming array value is a duplicate and should be skipped
            continue o;
         }
      }
      //If the value hasn't already been added to the return array (not a duplicate) then add it
      r[r.length] = a[i];
   }
   //Return the reconstructed array of unique values
   return r;
}

Use ArrayUtil for server-side scripts

ServiceNow now includes an ‘ArrayUtil’ script include that has a bunch of useful functions that you can use with server-side scripts. Among these is the ‘unique’ function. ‘unique’ can be used to eliminate duplicates from an array and is much cleaner for server-side script.

var uniqueArray = new ArrayUtil().unique(your_array);