Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javascript-array JavaScript array remove duplicate

Related Tutorials


 
 

Share on Google+Share on Google+

JavaScript array remove duplicate

Advertisement
In JavaScript array we can also detect and remove the duplicate elements by creating a user defined function.

JavaScript array remove duplicate

     

In JavaScript array we can also detect and remove the duplicate elements by creating a user defined function. Since there is not any built-in method in the JavaScript array to remove duplicate elements therefore we have created a function which will remove all the duplicated elements from the passed array. Code for the defined function is as given below:

 

 

 

 

 

 

function removeDuplicateElement(arrayName)
  {
  var newArray=new Array();
  label:for(var i=0; i<arrayName.length;i++ )
  {  
  for(var j=0; j<newArray.length;j++ )
  {
  if(newArray[j]==arrayName[i]) 
  continue label;
  }
  newArray[newArray.length] = arrayName[i];
  }
  return newArray;
  }

In this removeDuplicateElement(arrayName) we have passed the arrayName (Array Object) at which the operation is to be performed. Here is the full HTML example code as follows:

javascript_array_remove_duplicate.html

<html>
  <head>
  <title>
  JavaScript array remove duplicate element
  </title>
  <script type="text/javascript">
  var arr = new Array(7);
  arr[0]="Rose";
  arr[1]="India";
  arr[2]="Technologies";
  arr[3]="Pvt";
  arr[4]="Ltd";
  arr[5]="India";
  arr[6]="Rose";
  function removeDuplicateElement(arrayName)
  {
  var newArray=new Array();
  label:for(var i=0; i<arrayName.length;i++ )
  {  
  for(var j=0; j<newArray.length;j++ )
  {
  if(newArray[j]==arrayName[i]) 
  continue label;
  }
  newArray[newArray.length= arrayName[i];
  }
  return newArray;
  }
  document.writeln("Array before calling removeDuplicateElement()
   method is =<b>"
+arr+"</b></br>");
  document.writeln("Array after calling removeDuplicateElement()
   method is =<b>"
+removeDuplicateElement(arr)+"</b></br>");
  </script>
  </head>
<body bgcolor="#ddcdff">
  <h2>
  JavaScript Array Remove duplicate element
  </h2>
</body>
</html>

Output of example:

Download Sample Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 10, 2008

Related Tutorials

Discuss: JavaScript array remove duplicate   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
kiran
June 29, 2011
good logic

thank you, giving this code
TeMc
March 10, 2012
javascript-array-remove-duplicat

How dare you smear the internet with such crappy scripting examples.
Kumar
December 20, 2013
javascript

i have diff one <!DOCTYPE html> <html> <head> <script> function checkDuplicate(){ alert('inside into function'); var mydata = document.getElementById("test").value; alert(' my data : '+mydata); var N = new Array(); N= mydata.split(" "); alert('array length is : '+N.length); var duplicateVar=''; for (var i = N.length-1; i>=0 ; i--) { //alert('n[i] : '+N[i]); var count=0; for(var j = 0; j<N.length;j++){ alert("Ni : "+N[i]+ " ## "+"Nj : "+N[j]) if(N[i]==N[j]){ count = count+1; } if(count>1){ alert("duplicate found : "+N[j]); document.getElementById("duplicate").value=document.getElementById("duplicate").value+" "+N[j]; alert('j is going to deleted :'+N[j]); N.splice(j,1); alert('Now length is :'+N.length); } } } } </script> </head> <body> <form name="myform" onSubmit="return checkDuplicate()"> <input type="text" name="test" id="test" value="" size="50"><br> <input type="text" name="duplicate" id="duplicate" value="" size="50"><br> <input type="submit" name="submit" value="SUBMIT" > </form> </body> </html>
DMCA.com