2

How do I remove duplicates from a comma delimited string of integers using jquery/javascript?

My string is: 1,2,2,4,2,4,3,2,3,1,5,5,5,1,1,2

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Tommy Arnold
  • 3,339
  • 8
  • 31
  • 40
  • 1
    Duplicates of what? Please provide an example. I believe this might help: http://stackoverflow.com/questions/1960473/unique-values-in-an-array – Felix Kling Jul 10 '12 at 16:08
  • see these http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array http://stackoverflow.com/questions/3838845/jquery-remove-duplicate-li – Waqar Janjua Jul 10 '12 at 16:12
  • Split the string to create an array and follow the link I posted in my previous comment. – Felix Kling Jul 10 '12 at 16:12
  • see this post http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – Waqar Janjua Jul 10 '12 at 16:13
  • There's also: http://stackoverflow.com/questions/5506920/removing-duplicate-strings-using-javascript. – Jason Towne Jul 10 '12 at 16:13

8 Answers8

3

For one method, which doesn't demand writing the functionality from scratch or using some other js library check out duck-punching-with-jquery. (look for 'Example 2: $.unique() enhancement')

If you're willing to try some other library, the excellent underscorejs includes a 'uniq' method which accomplishes just what you need.

biril
  • 1,975
  • 1
  • 19
  • 30
3
function spit(){
           var answer = ("1,2,2,4,2,4,3,2,3,1,5,5,5,1,1,2").split(',').filter(function(item, pos,self) {
              return self.indexOf(item) == pos;
           });
}
2

Here's a simple example.

var str1 = "a,b,c,a,d,e,b";
var characters = str1.split(",");
var distinctCharacters = [];
jQuery.each(characters, function(index, c) {
if (jQuery.inArray(c, distinctCharacters) > -1) {
        // do nothing
        alert("already exists " + c);
    } else {
        distinctCharacters.push(c);
    }
});
alert(distinctCharacters);​
devang
  • 5,376
  • 7
  • 34
  • 49
1

From this post Remove Duplicates from JavaScript Array

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
 if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
  });
Community
  • 1
  • 1
Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36
1

good answers, there is also _.uniq() from the library underscore.js

mindandmedia
  • 6,800
  • 1
  • 24
  • 33
0

If they are only integers, you could have something like that:

var array = "1,2,2,4,2,4,3,2,3,1,5,5,5,1,1,2".split(",");

array.sort(function(a, b) { return a - b });

var uniqueArray = [array[0]];

for (var i = 1; i < array.length; i++) {
    if (array[i - 1] !== array[i])
        uniqueArray.push(array[i]);
}

console.log(uniqueArray);
ZER0
  • 24,846
  • 5
  • 51
  • 54
0

you can create a function in your javascript as follows:

Array.prototype.removeDuplicates = function () {
                return this.filter(function (item, index, self) {
                    return self.indexOf(item) == index;
                });
            };

then add .removeDuplicates() to the field you want the duplicates to be removed from after you split using the delimiter, such as:

var item = $("#inInputTextbox").val().split(",").removeDuplicates();

in your case, if i understand correctly, it could simply be:

var item = [a,b,c,d,d,d,c,b,a].split(",").removeDuplicates();

hope this helps.

andy
  • 1
-1
var numbersString = "1,2,3,4,5,6";
var numbersArray = numbers.split(',');

Now just insert with check in another array the content of numbersArray

Damien Locque
  • 1,810
  • 2
  • 20
  • 42