Skip to content

Instantly share code, notes, and snippets.

@trisharia
Last active September 22, 2017 02:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trisharia/252c97a00439ee988d9f11803eb2a325 to your computer and use it in GitHub Desktop.
Save trisharia/252c97a00439ee988d9f11803eb2a325 to your computer and use it in GitHub Desktop.
Remove duplicates from an array
// VMware vRealize Orchestrator action sample
//
// Removes duplicates from an array, with the option to also sort the array
//
// For vRO 6.0+
//
// Action Inputs:
// a - Array/Any - Array with potential duplicates
// doSort - boolean - Sort the array also? Potentially better performance than unsorted algorithm
//
// Return type: Array/Any
var b = (doSort) ? removeDupesSorted(a) : removeDupesUnsorted(a);
return b;
///////////////////
function removeDupesSorted(a) {
var b = new Array();
if (!a) return null;
if (a.length != 0) {
a.sort();
b.push(a[0]);
for (var i = 0; i < a.length - 1; i++) {
if (a[i+1] != a[i]) b.push(a[i+1]);
}
}
return b;
}
function removeDupesUnsorted(a) {
var b = new Array();
if (!a) return null;
if (a.length != 0) {
for each (var x in a) {
if (!isPresent(x, b)) {
b.push(x);
}
}
}
return b;
}
// is value x present in array ?
function isPresent(x, a) {
if (a) {
for each (var y in a) {
if (x === y) {
return true;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment