Remove duplicate characters from string

前端 未结 28 728
猫巷女王i
猫巷女王i 2020-12-01 13:09

I have to make a function in JavaScript that removes all duplicated letters in a string. So far I\'ve been able to do this: If I have the word \"anaconda\" it shows me as a

相关标签:
28条回答
  • 2020-12-01 13:43
    var str = 'anaconda'.split('');
    var rmDup = str.filter(function(val, i, str){
         return str.lastIndexOf(val) === str.indexOf(val);
    });
    console.log(rmDup); //prints ["c", "o", "d"]
    

    Please verify here: https://jsfiddle.net/jmgy8eg9/1/

    0 讨论(0)
  • 2020-12-01 13:44

    Just wanted to add my solution for fun:

    function removeDoubles(string) {
      var mapping = {};
      var newString = '';
    
      for (var i = 0; i < string.length; i++) {
        if (!(string[i] in mapping)) {
          newString += string[i];
          mapping[string[i]] = true;
        }
      }
      return newString;
    }
    
    0 讨论(0)
  • 2020-12-01 13:45

    I have 3 loopless, one-line approaches to this.

    Approach 1 - removes duplicates, and preserves original character order:

    var str = "anaconda";
    var newstr = str.replace(new RegExp("[^"+str.split("").sort().join("").replace(/(.)\1+/g, "").replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&")+"]","g"),"");
    //cod
    

    Approach 2 - removes duplicates but does NOT preserve character order, but may be faster than Approach 1 because it uses less Regular Expressions:

    var str = "anaconda";
    var newstr = str.split("").sort().join("").replace(/(.)\1+/g, "");
    //cdo
    

    Approach 3 - removes duplicates, but keeps the unique values (also does not preserve character order):

    var str = "anaconda";
    var newstr = str.split("").sort().join("").replace(/(.)(?=.*\1)/g, "");
    //acdno
    
    0 讨论(0)
  • 2020-12-01 13:45
        function RemDuplchar(str)
    {
        var index={},uniq='',i=0;
        while(i<str.length)
        {
            if (!index[str[i]])
            {
             index[str[i]]=true;
             uniq=uniq+str[i];
            }
                i++;
        }
        return uniq;
    } 
    
    0 讨论(0)
  • 2020-12-01 13:46

    Just came across a similar issue (finding the duplicates). Essentially, use a hash to keep track of the character occurrence counts, and build a new string with the "one-hit wonders":

    function oneHitWonders(input) {
        var a = input.split('');
        var l = a.length;
        var i = 0;
        var h = {};
        var r = "";
    
        while (i < l) {
            h[a[i]] = (h[a[i]] || 0) + 1;
    
            i += 1;
        }
    
        for (var c in h) {
            if (h[c] === 1) {
                r += c;
            }
        }
    
        return r;
    }
    

    Usage:

    var a = "anaconda";
    var b = oneHitWonders(a); // b === "cod"
    
    0 讨论(0)
  • 2020-12-01 13:46

    We can remove the duplicate or similar elements in string using for loop and extracting string methods like slice, substring, substr

    Example if you want to remove duplicate elements such as aababbafabbb:

    var data = document.getElementById("id").value
    for(var i = 0; i < data.length; i++)
    {
        for(var j = i + 1; j < data.length; j++)
        {
            if(data.charAt(i)==data.charAt(j))
            {
                data = data.substring(0, j) + data.substring(j + 1);
                j = j - 1;
                console.log(data);
            }
        }
    }
    

    Please let me know if you want some additional information.

    0 讨论(0)
提交回复
热议问题