Set in JS

Remove duplicates

by dpnminh

HTML

<div>
  <div id="currentArry">
    
  </div>
  <div id="resultArry">
    
  </div>
</div>
<button onclick="perform()">
Remove Duplicate
</button>

JavaScript

function removeDuplicate(arr){
	var set = new Set(arr);
  
  return [...set];// Use the spread operator to transform a set into an Array.

}

function perform(){
	var arr = [1,1,1,1,1,5,4,3,2,3,4,5,6,78,9,0,5,4,3];
  document.getElementById('currentArry').innerHTML = "Current array: " + arr.toString();
  
  var result = removeDuplicate(arr);
  
  document.getElementById('resultArry').innerHTML = "Array after removing duplicates: " + result.toString();
}