2

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!

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.