Showing unique characters in a string only once

后端 未结 13 2258
-上瘾入骨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:50

    <script>
        uniqueString = "";
    
        alert("Displays the number of a specific character in user entered string and then finds the number of unique characters:");
    
        function countChar(testString, lookFor) {
            var charCounter = 0;
            document.write("Looking at this string:<br>");
    
            for (pos = 0; pos < testString.length; pos++) {
                if (testString.charAt(pos) == lookFor) {
                    charCounter += 1;
                    document.write("<B>" + lookFor + "</B>");
                } else
                    document.write(testString.charAt(pos));
            }
            document.write("<br><br>");
            return charCounter;
        }
    
        function findNumberOfUniqueChar(testString) {
            var numChar = 0,
                uniqueChar = 0;
            for (pos = 0; pos < testString.length; pos++) {
                var newLookFor = "";
                for (pos2 = 0; pos2 <= pos; pos2++) {
                    if (testString.charAt(pos) == testString.charAt(pos2)) {
                        numChar += 1;
                    }
                }
                if (numChar == 1) {
                    uniqueChar += 1;
                    uniqueString = uniqueString + " " + testString.charAt(pos)
                }
                numChar = 0;
            }
            return uniqueChar;
        }
    
        var testString = prompt("Give me a string of characters to check", "");
        var lookFor = "startvalue";
        while (lookFor.length > 1) {
            if (lookFor != "startvalue")
                alert("Please select only one character");
            lookFor = prompt(testString + "\n\nWhat should character should I look for?", "");
        }
        document.write("I found " + countChar(testString, lookFor) + " of the<b> " + lookFor + "</B> character");
        document.write("<br><br>I counted the following " + findNumberOfUniqueChar(testString) + " unique character(s):");
        document.write("<br>" + uniqueString)
    </script>

    0 讨论(0)
  • 2020-12-31 20:50

    Here is the simplest function to do that pt. 2

    const showUniqChars = (text) => {
      let uniqChars = "";
    
      for (const char of text) {
        if (!uniqChars.includes(char))
          uniqChars += char;
      }
      return uniqChars;
    };
    
    0 讨论(0)
  • 2020-12-31 20:54

    Per the actual question: "if the letter doesn't repeat its not shown"

    function unique_char(str)
    {
        var obj = new Object();
    
        for (var i = 0; i < str.length; i++)
        {
            var chr = str[i];
            if (chr in obj)
            {
                obj[chr] += 1;
            }
            else
            {
                obj[chr] = 1;
            }
        }
    
        var multiples = [];
        for (key in obj)
        {
            // Remove this test if you just want unique chars
            // But still keep the multiples.push(key)
            if (obj[key] > 1)
            {
                multiples.push(key);
            }
        }
    
        return multiples.join("");
    }
    
    var str = "aaabbbccc";
    document.write(unique_char(str));
    
    0 讨论(0)
  • 2020-12-31 21:02

    Try this if duplicate characters have to be displayed once, i.e., for i/p: aaabbbccc o/p: abc

    var str="aaabbbccc";
    Array.prototype.map.call(str, 
      (obj,i)=>{
        if(str.indexOf(obj,i+1)==-1 ){
         return obj;
        }
      }
    ).join("");
    //output: "abc"
    

    And try this if only unique characters(String Bombarding Algo) have to be displayed, add another "and" condition to remove the characters which came more than once and display only unique characters, i.e., for i/p: aabbbkaha o/p: kh

    var str="aabbbkaha";
    Array.prototype.map.call(str, 
     (obj,i)=>{
       if(str.indexOf(obj,i+1)==-1 && str.lastIndexOf(obj,i-1)==-1){ // another and condition
         return obj;
       }
     }
    ).join("");
    //output: "kh"
    
    0 讨论(0)
  • 2020-12-31 21:04

    The one line solution will be to use Set. const chars = [...new Set(s.split(''))];

    0 讨论(0)
  • 2020-12-31 21:05

    You can use a regular expression with a custom replacement function:

    function unique_char(string) {
        return string.replace(/(.)\1*/g, function(sequence, char) {
             if (sequence.length == 1) // if the letter doesn't repeat
                 return ""; // its not shown
             if (sequence.length == 2) // if its repeated once
                 return char; // its show only once (if aa shows a)
             if (sequence.length == 3) // if its repeated twice
                 return sequence; // shows all(if aaa shows aaa)
             if (sequence.length == 4) // if its repeated 3 times
                 return Array(7).join(char); // it shows 6( if aaaa shows aaaaaa)
             // else ???
             return sequence;
        });
    }
    
    0 讨论(0)
提交回复
热议问题