Remove duplicate characters from string

前端 未结 28 729
猫巷女王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:37
    1. If you want your function to just return you a unique set of characters in your argument, this piece of code might come in handy. Here, you can also check for non-unique values which are being recorded in 'nonUnique' titled array:

      function remDups(str){
          if(!str.length)
              return '';
          var obj = {};
          var unique = [];
          var notUnique = [];
          for(var i = 0; i < str.length; i++){
              obj[str[i]] = (obj[str[i]] || 0) + 1;
          }
          Object.keys(obj).filter(function(el,ind){
              if(obj[el] === 1){
                  unique+=el;
              }
              else if(obj[el] > 1){
                  notUnique+=el;
              }
          });
      return unique;
      }
      console.log(remDups('anaconda')); //prints 'cod'
      
    2. If you want to return the set of characters with their just one-time occurrences in the passed string, following piece of code might come in handy:

      function remDups(str){
          if(!str.length)
              return '';
          var s = str.split('');
          var obj = {};
          for(var i = 0; i < s.length; i++){
              obj[s[i]] = (obj[s[i]] || 0) + 1;
          }
          return Object.keys(obj).join('');
      }
      console.log(remDups('anaconda')); //prints 'ancod'
      
    0 讨论(0)
  • 2020-12-01 13:40

    function find_unique_characters(str) {
      var unique = '';
      for (var i = 0; i < str.length; i++) {
        if (str.lastIndexOf(str[i]) == str.indexOf(str[i])) {
          unique += str[i];
        }
      }
      return unique;
    }
    
    console.log(find_unique_characters('baraban'));
    console.log(find_unique_characters('anaconda'));

    If you only want to return characters that appear occur once in a string, check if their last occurrence is at the same position as their first occurrence.

    Your code was returning all characters in the string at least once, instead of only returning characters that occur no more than once. but obviously you know that already, otherwise there wouldn't be a question ;-)

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

    You can put character as parameter which want to remove as unique like this

    function find_unique_characters(str, char){
      return [...new Set(str.split(char))].join(char);
    }
    

    function find_unique_characters(str, char){
      return [...new Set(str.split(char))].join(char);
    }
    
    let result = find_unique_characters("aaaha ok yet?", "a");
    console.log(result);

    0 讨论(0)
  • 2020-12-01 13:40
    function removeDup(str) {
      var arOut = [];
      for (var i=0; i < str.length; i++) {
        var c = str.charAt(i);
        if (c === '_') continue;
        if (str.indexOf(c, i+1) === -1) {
          arOut.push(c);
        }
        else {
          var rx = new RegExp(c, "g");
          str = str.replace(rx, '_');
        }
      }
      return arOut.join('');
    }
    
    0 讨论(0)
  • 2020-12-01 13:42

    For strings (in one line)

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

    For arrays (in one line)

    removeDuplicatesArr = arr => [...new Set(arr)]

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

    Yet another way to remove all letters that appear more than once:

    function find_unique_characters( string ) {
        var mapping = {};
        for(var i = 0; i < string.length; i++) {
            var letter = string[i].toString();
            mapping[letter] = mapping[letter] + 1 || 1;
        }
        var unique = '';
        for (var letter in mapping) {
            if (mapping[letter] === 1)
                unique += letter;
        }
    
        return unique;
    }
    

    Live test case.

    Explanation: you loop once over all the characters in the string, mapping each character to the amount of times it occurred in the string. Then you iterate over the items (letters that appeared in the string) and pick only those which appeared only once.

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