Last active
September 22, 2017 02:44
-
-
Save trisharia/252c97a00439ee988d9f11803eb2a325 to your computer and use it in GitHub Desktop.
Remove duplicates from an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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