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.

This question already has an answer here:

I'm looking for an easy way of removing a duplicate value from an array. I figured out how to detect if there is a duplicate or not, just I don't know how to "push" it from the value. For example, if you go to the link provided, and then type, "abca" (press return/enter key after each letter).. it will alert "duplicate!"

But I also want to figure out how to remove that duplicate from the textarea?

http://jsfiddle.net/P3gpp/

This is the part that seems to not be working ::

sort = sort.push(i);
textVal = sort;
return textVal;
share|improve this question

marked as duplicate by Bergi Jul 2 '14 at 11:07

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.

7 Answers 7

up vote 2 down vote accepted

These are the functions I created/use for removing duplicates:

var removeDuplicatesInPlace = function (arr) {
    var i, j, cur, found;
    for (i = arr.length - 1; i >= 0; i--) {
        cur = arr[i];
        found = false;
        for (j = i - 1; !found && j >= 0; j--) {
            if (cur === arr[j]) {
                if (i !== j) {
                    arr.splice(i, 1);
                }
                found = true;
            }
        }
    }
    return arr;
};

var removeDuplicatesGetCopy = function (arr) {
    var ret, len, i, j, cur, found;
    ret = [];
    len = arr.length;
    for (i = 0; i < len; i++) {
        cur = arr[i];
        found = false;
        for (j = 0; !found && (j < len); j++) {
            if (cur === arr[j]) {
                if (i === j) {
                    ret.push(cur);
                }
                found = true;
            }
        }
    }
    return ret;
};

So using the first one, this is how your code could look:

function cleanUp() {
    var text = document.getElementById("fld"),
        textVal = text.value,
        array;

    textVal = textVal.replace(/\r/g, " ");
    array = textVal.split(/\n/g);

    text.value = removeDuplicatesInPlace(array).join("\n");
}

DEMO: http://jsfiddle.net/VrcN6/1/

share|improve this answer
    
Thank you. This works very well! –  Matthew Aug 2 '13 at 3:13

Why do it the hard way, it can be done more easily using javascript filter function which is specifically for this kind of operations:

var arr = ["apple", "bannana", "orange", "apple", "orange"];

arr = arr.filter( function( item, index, inputArray ) {
           return inputArray.indexOf(item) == index;
    });


---------------------
Output: ["apple", "bannana", "orange"]
share|improve this answer
1  
I think that, in 2014, five years into the EcmaScript 5 era and with IE8 below 10% of world browser share, we can safely call this the correct answer. If one is concerned with support for old versions of IE, a polyfill library for EC5 can be used. –  Semicolon Feb 1 '14 at 17:02
1  
(I would note though that while 'filter' is the right answer 90% of the time, it isn't appropriate on its own in circumstances where there may be references to the original array elsewhere because it creates a new array and the original array object is left intact.) –  Semicolon Feb 1 '14 at 17:09

You can use Array.reduce() to remove the duplicates. You need a helper object to keep track of how many times an item has been seen.

function cleanUp() 
{
    var textBox = document.getElementById("fld"),
    array = textBox.value.split(/\r?\n/g),
    o = {},
    output;

    output = array.reduce(function(prev, current) {
        var key = '$' + current;

        // have we seen this value before?
        if (o[key] === void 0) {
            prev.push(current);
            o[key] = true;
        }

        return prev;
    }, []);

    // write back the result
    textBox.value = output.join("\n");
}

The output of the reduce() step can be used directly to populate the text area again, without affecting the original sort order.

Demo

share|improve this answer

You can do this easily with just an object:

function removeDuplicates(text) {
    var seen = {};
    var result = '';

    for (var i = 0; i < text.length; i++) {
        var char = text.charAt(i);

        if (char in seen) {
            continue;
        } else {
            seen[char] = true;
            result += char;
        }
    }

    return result;
}

function cleanUp() {
    var elem = document.getElementById("fld");

    elem.value = removeDuplicates(elem.value);
}
share|improve this answer
    
Consider: result += char in seen? '' : seen[char] = char; –  RobG Aug 2 '13 at 3:13

Set is a very handy thing to use when you dont want any duplicates to begin with.

According to Mimicking sets in JavaScript? Objects can be uset do mimick sets.

share|improve this answer
    
This should be a comment, since it doesn't really answer the question. –  Bergi Jul 2 '14 at 11:08
      arr3 = [1, 2, 3, 2, 4, 5];

      unique = [];

   function findUnique(val)
  {
   status = '0';
   unique.forEach(function(itm){

      if(itm==val)
      { 
      status=1;
      }

              })
 return status;
 }

 arr3.forEach(function(itm){

  rtn =  findUnique(itm);
  if(rtn==0)
  unique.push(itm);


  });

  console.log(unique);  // [1, 2, 3, 4, 5]
share|improve this answer

Based on user2668376 solution, this will return a new array without duplicates.

Array.prototype.removeDuplicates = function () {
    return this.filter(function (item, index, self) {
        return self.indexOf(item) == index;
    });
};
share|improve this answer
    
So, if user2668376 already has answered with this solution, why repeat it? Btw, you have a self too much. –  Bergi Jul 2 '14 at 11:02
    
His solution is without .prototype. In my opinion it is much better to use that. I deleted the var self = this; –  Frank Roth Jul 2 '14 at 13:12

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