Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
How to print the vowels of the input str without duplicates in JavaScript

This is my code thus far, it duplicates vowels e.g.:

  • Input: baloon
  • Current output: a,o,o
  • Expected output: a,o

How can I fix this?

function printOutVowels(str) {
  let vowels = "";
  for (let i = 0; i < str.length; i++) {
    if (str[i] == "a" || str[i] == "e" || str[i] == "i" || str[i] == "o" || str[i] == "u") {
      vowels = str[i]
      console.log(vowels);
    }
  }
}

printOutVowels("timidity");

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

One solution is using Set data structure. A set is like an array, but contains no duplicated element.

function printOutVowels(str) {
  // Create an empty set
  const vowels = new Set();
  
  for (let i = 0; i < str.length; i++) {
    if (str[i] == "a" || str[i] == "e" || str[i] == "i" || str[i] == "o" || str[i] == "u") {
      // Add element to the set
      vowels.add(str[i]);
    }
  }

  // Print the set content
  for (let item of vowels) {
    console.log(item);
  }
}

printOutVowels("timidity");

Behind the scene, Set use its has method to check if an item is already in the set. This method is faster than Array.prototype.includes when a set and an array have the same size.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could do that :

function printOutVowels(str){
    let vowels = "aeiou";
    let output = "";
    for (let i = 0; i < vowels.length; i++) {
      if(str.toLowerCase().includes(vowels[i])) output += vowels[i];
    }
    return output;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Splitting and filtering is a concise way to get the vowels. Running them through a set will enforce uniqueness. (Force to lowercase to handle both cases)

function vowels(str) {
  const isVowel = l => /^[aeiou]$/.test(l);
  const vowels = new Set(str.toLowerCase().split('').filter(isVowel));
  return Array.from(vowels.values());
}

console.log(vowels('TIMidity'))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda