0

Here is my code. What should I modify of this code to get the output as "T-1 r-1 a-1 e-1 "

(other characters are repeating. So no need to print the others)

function different() {

    var retureArr = [];
    var count = 0;
    var complete_name = "Trammell";
    var stringLength = complete_name.length;

    for (var t = 0; t < stringLength; t++) {
        for (var s = 0; s < stringLength; s++) {
            var com1 = complete_name.charAt(t);
            var com2 = complete_name.charAt(s);

            if (com1 != com2) {
                retureArr[count] = com1;
                count++;
            }
        }

        count = 0;
    }
}

1
  • Please state what is your requirement. Not what the desired output is. Also, note that your function does not return nor print a thing. Commented Mar 15, 2015 at 15:53

2 Answers 2

1

I think this is what you want. You need to count the number of occurrences of each character in a dictionary. Then you can print them based on the count being equal to 1.

var retureArr = [];
var complete_name = "Trammell";

for (var i = 0; i < complete_name.length; i++)
{
    var key = complete_name[i];
    if (!(key in retureArr))
    {
        retureArr[key] = 1;
    }
    else
    {
        retureArr[key] = retureArr[key] + 1;
    }
}

var output = "";
for (var key in retureArr)
{
    if (retureArr[key] == 1)
    {
        output += key + "-" + retureArr[key] + " ";
    }
}

alert(output);

This alerts the following string:

T-1 r-1 a-1 e-1
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much Millie Smith. It's working well. If i want to print other repeating characters also what modification should i do?
What do you mean by "other repeating characters"? You could remove the last if statement that checks if the count is 1.
It means actually repeating characters. Here your code prints non-repeating characters only.
Ok Millie Smith I modified it as i want. Thank you sir!!
What attribute can i keep as a unique id or something else in html elements. It shouldn't be able to modify by the browser.
|
0

This works. but perhaps isn't the most efficient!

var string = "input string";
var stringList = [];
var outputString = "";

for (var i=0; i < string.length; i++){
    var charObject = {"Char": string.charAt(i), "Passed": false};   
    stringList.push(charObject);
}


for (var i=0; i < stringList.length; i++){
    if(!stringList[i].Passed && stringList[i].Char != " "){
        var currentCount = countOccurrences(string, stringList[i].Char);
        if(currentCount == 1){
            outputString += stringList[i].Char+"-"+currentCount + " ";
        }
        stringList[i].Passed = true;
    }
}

console.log(outputString);

function countOccurrences(string, char){
    var count = 0;
    for (var i=0; i < string.length; i++){
        if(string.charAt(i) == char){
            count++;
        }
    }
    return count;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.