Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

79
Visualizações
To find unique characters in a string using javascript

Write a JavaScript program to find the unique characters in a string. The function uniqueCharacters must take a string as its argument and return the same after removing all the characters that appear more than once in the string. This is the main function.

The function modifyString is the sub-function. This is invoked from within the function uniqueCharacters with the same string as the argument. This function will remove all the white space characters & convert the entire string to lower case and return the same to the caller.

Note: You may verify the correctness of your function by invoking it from within console.log

console.log(uniqueCharacters("Welcome to the Javascript course"));

Where am I going wrong? My code (below) isn't working correctly. The output should be: welcomthjavsripu.

function modifyString(str) {
    return str.charAt(0).toLowerCase() + str.slice(1);
}

function uniqueCharacters(str)
{
   modifyString(str);
   var str1=str;
   var uniql= "";
   for(var x=0;x<str1.length;x++)
   {
       if(uniql.indexOf(str1.charAt(x))==-1)
       {
           if(str.charAt(x) === ' ')
           {
               continue;
           }
           uniql += str1[x];
       }
   }
   return uniql;
}
console.log(uniqueCharacters("Welcome to the Javascript course"));
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I see two issues with your code:

  • First you need to replace modifyString(str) with str = modifyString(str)
  • Second you only call toLower on the first character of your string, but you expect your whole string to be lowercase. The modifyString function should simply be return str.toLowerCase();
about 4 years ago · Juan Pablo Isaza Relatório

0

I would use the approach of counting characters with Map and then filter them.

function uniqueCharacters(str)
{
   const uniqChar = new Map();
   const result = []
   for(const character of str) {
     if(uniqChar.has(character)) {
       uniqChar.set(character, uniqChar.get(uniqChar) + 1)
     } else {
       uniqChar.set(character,1)
     }
   }
   uniqChar.forEach((val,key) => {
      if(val === 1) result.push(key)
   })
   return result;
}
console.log(uniqueCharacters("AbaacddeR"));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda