Remove duplicate characters from string

前端 未结 28 730
猫巷女王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:47

    With lodash:

    _.uniq('baraban').join(''); // returns 'barn'
    
    0 讨论(0)
  • 2020-12-01 13:47

    function removeduplicate(str) {

    let map = new Map();
    // n 
    for (let i = 0; i < str.length; i++) {
        if (map.has(str[i])) {
            map.set(str[i], map.get(str[i]) + 1);
        } else {
            map.set(str[i], 1);
        }
    
    }
    
    
    let res = '';
    
    for (let i = 0; i < str.length; i++) {
        if (map.get(str[i]) === 1) {
            res += str[i];
        }
    }
    
    // o (2n) - > O(n)
    
    // space o(n)
    
    return res;
    

    }

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

    We can also now clean things up using filter method:

    function removeDuplicateCharacters(string) {
      return string
        .split('')
        .filter(function(item, pos, self) {
          return self.indexOf(item) == pos;
        })
        .join('');
    }
    console.log(removeDuplicateCharacters('baraban'));
    

    Working example:

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

    I have FF/Chrome, on which this works:

    var h={}; 
    "anaconda".split("").
      map(function(c){h[c] |= 0; h[c]++; return c}).
      filter(function(c){return h[c] == 1}).
      join("")
    

    Which you can reuse if you write a function like:

    function nonRepeaters(s) {
      var h={}; 
      return s.split("").
        map(function(c){h[c] |= 0; h[c]++; return c}).
        filter(function(c){return h[c] == 1}).
        join("");
     }
    

    For older browsers that lack map, filter etc, I'm guessing that it could be emulated by jQuery or prototype...

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

    Using Set:

    removeDuplicates = str => [...new Set(str)].join('');
    

    Thanks to David comment below.

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

    This code worked for me on removing duplicate(repeated) characters from a string (even if its words separated by space)

    Link: Working Sample JSFiddle

    /* This assumes you have trim the string and checked if it empty */
    function RemoveDuplicateChars(str) {
       var curr_index = 0;
       var curr_char;
       var strSplit;
       var found_first;
       while (curr_char != '') {
          curr_char = str.charAt(curr_index);
          /* Ignore spaces */
          if (curr_char == ' ') {
             curr_index++;
             continue;
          }
          strSplit = str.split('');
          found_first = false;
          for (var i=0;i<strSplit.length;i++) {
             if(str.charAt(i) == curr_char && !found_first) 
                found_first = true;
             else if (str.charAt(i) == curr_char && found_first) {
                /* Remove it from the string */
                str = setCharAt(str,i,'');
             }
          }
          curr_index++;
       }
       return str;
    }
    function setCharAt(str,index,chr) {
        if(index > str.length-1) return str;
        return str.substr(0,index) + chr + str.substr(index+1);
    }
    
    0 讨论(0)
提交回复
热议问题