How can I push duplicate values in to an array

Manasa23
Tera Contributor

HI,
I'm working on GlideExcelParser API, How to Find the Duplicates in the Excel for specific columns like Asset tag and Serial Number ???

My Requirement is to Store all the Assets tag and Row Number  into an  Array and  to push only the Duplicates into Separate Array.

Can someone help me  ??


Thanks,
M

2 REPLIES 2

Sai Kumar B
Mega Sage
Mega Sage

@Manasa 

To remove duplicates in the array, You can use OOB ArrayUtil Script Include

 var noDuplicates = new ArrayUtil().unique(arr);//arr contains the array values

To add Duplicates into the new array, try the below

var duplicates = []

for (var i = 0; i < yourOrginalArray.length; i++) {
  if (yourOrginalArray[i + 1] === yourOrginalArray[i]) {
    duplicates.push(yourOrginalArray[i]) //Push duplicates into new array
  }
}

Manasa23
Tera Contributor

HI Sai,

Tried below script, it is not working .

var obj = [                            // left side : Row Number  // Right side : Asset tag 
{ "1": "assetag100" },
{ "2": "assetag101" },
{ "3": "assetag100" },
{ "4": "assetag1001" },
{ "5": "assetag101" },
];
var arr = new Array();
arr.push(obj);
var v_aAssetTagSorted = arr.sort();
gs.print(JSON.stringify(v_aAssetTagSorted));

var duplicates = [];

for (var i = 0; i < v_aAssetTagSorted.length; i++) {
if (v_aAssetTagSorted[i + 1] === v_aAssetTagSorted[i].assettag) {
duplicates.push(v_aAssetTagSorted[i].assettag) //Push duplicates into new array
}
}

gs.print(JSON.stringify(duplicates));