Search code examples
javascriptjquerycsvunique

Remove Duplicates From Comma Delimited String


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


Solution

  • 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.