Array Remove Duplicate Results from array - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

Array Remove Duplicate Results from array

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {/*from ww w .  j a v  a2 s .c o m*/
function unique(arr) {
  var unique = [],
      map = {};
  for (var i = 0; i < arr.length; i++) {
    var exists = map[arr[i]];
      console.log(exists);
    if (!exists)  {
        unique.push(arr[i]);
        map[arr[i]] = true;
    }
  }
  return unique;
}
document.write(unique([1,1,1,1,1,1,3,3,3,3,3,3,2,2,1,4,4,3,5,5]));
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials