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

171
Visualizações
How to search a string for the most common character or word in JavaScript

I have a question that asked me to:

Clean the following text and find the most frequent word: "const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; &as& mo@re rewarding as educa@ting &and& @emp%o@weri@ng peo@ple. ;I found tea@ching m%o@re interesting tha@n any ot#her %jo@bs. %Do@es thi%s mo@tiv#ate yo@u to be a tea@cher!? %Th#is 30#Days&OfJavaScript &is al@so $the $resu@lt of &love& of tea&ching'"

I used regEx to clean the string like this:

const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; && mo@re rewarding than educa@ting &and& @emp%o@weri@ng peo@ple. ;I find tea@ching m%o@re interesting tha@n any ot#her %jo@bs. %Do@es thi%s mo@tiv#ate yo@u to be a tea@cher!? %Th#is 30#Days&OfJavaScript &is al@so $the $resu@lt of &love& of tea&ching';
const sentReg = /\W(?<!1)/g;
let sent = sentence.replace(/ /g, "1");

let finalSent = sent.replace(sentReg, ""), finalfinalSent = finalSent.replace(/1/g, " ");

I realized I don't know how to (or there might not be a way to) use the match() function to search a string by word, so I tried splitting it up into an array like:

let senArr = finalfinalSent.split(" "), wordOccur = [];

for (const x of senArr) {
    var re = new RegExp(x, "g");
    var y = finalfinalSent.match(re);

    wordOccur = wordOccur.concat([y.length]);
};

... and now I'm stuck because I don't know how to search an array in JavaScript, only in Python, and I feel the way to search through a string would be much easier than this. I would appreciate some pointers.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I wouldn't change the spaces to "1". Instead use a regex that will not remove spaces while cleaning.

Then you can call match on the cleaned string, and use reduce to start counting words and maintain a reference to the most frequent one:

const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; && mo@re rewarding than educa@ting &and& @emp%o@weri@ng peo@ple. ;I find tea@ching m%o@re interesting tha@n any ot#her %jo@bs. %Do@es thi%s mo@tiv#ate yo@u to be a tea@cher!? %Th#is 30#Days&OfJavaScript &is al@so $the $resu@lt of &love& of tea&ching';

let word = sentence.replace(/[^\w\s]/g, "")
            .match(/\w+/g)
            .reduce((acc, word) => {
                acc[word] = (acc[word] || 0) + 1;
                if (!(acc[word] < acc[acc.$])) acc.$ = word;
                return acc;
            }, {}).$;
            
console.log(word);

Observe that acc will be a "dictionary" of words, where the corresponding values are the counts. A special $ entry is created in that same dictionary, which will hold the most frequent word.

If there is more than one word that has the maximum frequency, and you want to get all of these words, not just one, then return an array instead of a string:

const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; && mo@re rewarding than educa@ting &and& @emp%o@weri@ng peo@ple. ;I find tea@ching m%o@re interesting tha@n any ot#her %jo@bs. %Do@es thi%s mo@tiv#ate yo@u to be a tea@cher!? %Th#is 30#Days&OfJavaScript &is al@so $the $resu@lt of &love& of tea&ching';

let word = sentence.replace(/[^\w\s]/g, "")
            .match(/\w+/g)
            .reduce((acc, word) => {
                acc[word] = (acc[word] || 0) + 1;
                if (!(acc[word] <= acc[acc.$])) acc.$ = [word];
                else if (acc[word] === acc[acc.$]) acc.$.push(word);
                return acc;
            }, {}).$;
            
console.log(word);

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