简体   繁体   中英

Showing unique characters in a string only once

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

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 . so far my function works like this:

  • if the letter doesn't repeat, it's not shown
  • if it's repeated once, it's show only once (ie aa shows a)
  • if it's repeated twice, shows all (ie aaa shows aaa)
  • if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa)
function unique_char(string) {
    var unique = '';
    var count = 0;
    for (var i = 0; i < string.length; i++) {
        for (var j = i+1; j < string.length; j++) {
            if (string[i] == string[j]) {
                count++;
                unique += string[i];
            }
        }
    }
    return unique;
}

document.write(unique_char('aaabbbccc'));

The function must be with loop inside a loop; that's why the second for is inside the first.

if you want to return values in a array you can use this below function.

    const getUniqueChar=(str)=>str.split('').filter((item, index, arr)=> 
    arr.slice(index+1).indexOf(item)===-1);

    console.log(getUniqueChar("aaabbbccc"));

OR

You can use Set constructor

    cosnt getUniqueChar=(str)=>
    new Set(str.split(''));
    console.log(getUniqueChar('aaabbbccc'));
let unique=myArray.filter((item,index,array)=>array.indexOf(item)===index)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM