Remove duplicate in a string - javascript

With Set and Array.from this is pretty easy:

Array.from(new Set(x.split(','))).toString()

var x = "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double"
x = Array.from(new Set(x.split(','))).toString();
document.write(x);


If you have to support current browsers, you can split the array and then filter it

var x = "Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Int32,Double,Double,Double";
    
var arr = x.split(',');
    
x = arr.filter(function(value, index, self) { 
    return self.indexOf(value) === index;
}).join(',');

document.body.innerHTML = x;


Use new js syntax remove Dupicate from a string.

String.prototype.removeDuplicate = Function() {
  const set = new Set(this.split(','))
  return [...set].join(',')
}
x.removeDuplicate()

Tags:

Javascript