Showing unique characters in a string only once

后端 未结 13 2380
-上瘾入骨i
-上瘾入骨i 2020-12-31 20:38

I have a string with repeated letters. I want letters that are repeated more than once to show only once. For instance I have a string aaabbbccc i want the result to be abc.

13条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 20:45

    Here is the simplest function to do that

      function remove(text) 
        {
          var unique= "";
          for(var i = 0; i < text.length; i++)
          {
            if(unique.indexOf(text.charAt(i)) < 0) 
            {
              unique += text.charAt(i);
            }
          }
          return unique;
        }
    

提交回复
热议问题