# Remove Duplicated //JavaScript Repository

## Description

Removes duplicated values on an array.
Created: 2005.08.13 - Modified 2005.11.19

``````//+ Carlos R. L. Rodrigues
//@ http://jsfromhell.com/array/remove-duplicated [rev. #3]

removeDuplicated = function(a, s){
var p, i, j;
if(s) for(i = a.length; i > 1;){
if(a[--i] === a[i - 1]){
for(p = i - 1; p-- && a[i] === a[p];);
i -= a.splice(p + 1, i - p - 1).length;
}
}
else for(i = a.length; i;){
for(p = --i; p > 0;)
if(a[i] === a[--p]){
for(j = p; p-- && a[i] === a[p];);
i -= a.splice(p + 1, j - p).length;
}
}
return a;
};``````

## Example (Example)

``````<script type="text/javascript">
//<![CDATA[

a = [0,1,4,1,2,3,5,5,3,7,0,6,0,1];
b = a.slice().sort();

document.write(
"<br />removeDuplicated(A) = ", removeDuplicated(a),
"<br /><h2>Matriz pr?-ordenada</h2>B = ", b.join(","),
"<br />removeDuplicated(B, true) = ", removeDuplicated(b, true)
);

//]]>
</script>``````

## Help

removeDuplicated(vector: Array, [sorted: Boolean = false]): Array
Returns an array without the duplicated values.
vector
array that will be analyzed
sorted
pass true only if the array is already sorted, to obtain better performance