Tuesday, January 11, 2011

Remove duplicate elements from a comma seperated list (JAVASCRIPT)

 var input = 'a,b,b,said,said, t, u, ugly, ugly';
 var splitted = input.split(',');
 var collector = {};
 for (i = 0; i < splitted.length; i++) {
    key = splitted[i].replace(/^\s*/, "").replace(/\s*$/, "");
    collector[key] = true;
 }
 var out = [];
 for (var key in collector) {
    out.push(key);
 }
 var output = out.join(',');
 alert(output);

1 comment: