Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Possible Duplicate:
Remove duplicates from array

Could you please help me out. I have this piece of code..

   <html>
  <head>
  <title>test</title>
  <script>

  var array = new Array();

  function insert(val){
  array[array.length]=val;
  }

  function show() {
  var string="";
  for(i = 0; i < array.length; i++) {
  string =string+array[i]+' , ';
  }
  if(array.length > 0)
 document.getElementById('result').innerHTML = string;
  }

  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.getElementById('result1').innerHTML = newArray;
            }
  </script>

  </head>

  <body>
  <h2>Demo</h2>
  <form name="form1">
  <input id="userinput" type="text" name="Input"/>
  <input type="button" Value="Add" onclick="insert(getElementById('userinput').value),show();"/>
  <input type="button" Value="Substract" onclick="removeDuplicateElement(array);"/>
  </form>
   <textarea style="border:1px solid #B3B3B3; width:218px;" rows="2" cols="30" type="text"  id="result"></textarea>
   <textarea style="border:1px solid #B3B3B3; width:218px;" rows="2" cols="30" type="text"  id="result1"></textarea>
  </body>
</html>

And I was trying to implement the remove duplicate function I found on the internet without any success. (see below) What am I doing wrong?

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;
}

Thank you!

share|improve this question
2  
It's funny because this question is about duplicates. Anyway, that's a C# question, @David. –  minitech Oct 14 '12 at 15:03
2  
Please use indentation and proper spacing, it makes code a lot easier to read. –  minitech Oct 14 '12 at 15:04
 
@David: In that duplicate link I don't see javascript –  Muthu Kumaran Oct 14 '12 at 15:04
1  
Do check this answer: stackoverflow.com/a/840849/772450 –  arturhoo Oct 14 '12 at 15:05
 
@arturhoo that method will only work properly for arrays of strings. –  Matt Greer Oct 14 '12 at 15:08
show 2 more comments

marked as duplicate by 0x499602D2, KooiInc, flem, ChrisF, Woot4Moo Oct 14 '12 at 17:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 3 down vote accepted

A few (hopefully) useful code tips:

  1. Don't use new Array() unless you want to make an array that's a fixed size (unusual). Use [] instead.
  2. arr.push(item) is the same thing as arr[arr.length] = item, except better.
  3. That loop in your show function can be accomplished using array.join(' , ').

Anyway, the actual problem is here:

return newArray;
            document.getElementById('result1').innerHTML = newArray;

return exits the function. It looks like you want to take out the return part.

... but anyway, that code is pretty wasteful. I would write it as:

function removeDuplicateElements(array) {
    document.getElementById('result1').innerHTML =
        array.filter(function(item, index) {
            return array.indexOf(item) !== index;
        });
}

... assuming a modern browser that supports Array#indexOf and Array#filter.

share|improve this answer
1  
Thank you...It worked... –  Chris2012 Oct 14 '12 at 15:21
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.