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

135
Visualizações
It is possible to filter the array that have duplicate character in its element in Javascript?

Question Is is possible, to match a word that contains the same character in different positions of string array?

for example:- suppose i have the following array

arr1=["apply","application","apple","alphabet","killo","pipe"]

Now I want the following output of the element that have double 'p' (may be together or different position)

Desirable output:

arr1=["apply","application","apple","pipe"]

if i put 'l' then I should get output arr1=[killo] output

arr1=["apply","application","apple","alphabet","killo","pipe"]    
ans= arr1.filter(x => x.includes('p'))  
console.log(ans)

but i am getting following output

arr1=["apply","application","apple","alphabet","pipe"] 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use the following regex format:

var arr1 = ["apply", "application", "apple", "alphabet", "killo", "pipe"];

function filter_dup(arr, c) {
  return arr.filter(x => x.match(new RegExp(`.*${c}.*${c}.*`)));
}

console.log(filter_dup(arr1, 'p'));
console.log(filter_dup(arr1, 'l'));

You can also use String.prototype.indexOf and String.prototype.lastIndexOf (if the first and last indices of a character are different than there are at least two occurrences of that character):

var arr1 = ["apply", "application", "apple", "alphabet", "killo", "pipe"];

function filter_dup(arr, c) {
  return arr.filter(x => x.indexOf(c) != x.lastIndexOf(c));
}

console.log(filter_dup(arr1, 'p'));
console.log(filter_dup(arr1, 'l'));

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