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

207
Vistas
How to find characters that are there in one string and not there in another in javascript?

I want to find distinct characters that are there in one string and not in another. Suppose firstString contains ABC and secondString contains BC now output op1 should contain 'characters that are distinctly there in the firstString but not in the secondString' i.e. A and op2 should contain 'characters that are distinctly there in the secondString but not in the firstString' i.e. in this case null. If firstString is 'SBG' and secondString is 'BANGALORE' op1 should be 'S' op2 should be 'ANLORE'

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I would recommend using Javascripts Set and filtering out elements which are not present:

// returns the distinct characters found in string1
// but not in string2 as an array
function findDistinctChars(string1, string2) {
  const set1 = new Set(string1)
  const set2 = new Set(string2)
  return [...set1].filter(char => !set2.has(char))
}

// example usage
const s1 = "aaabcde"
const s2 = "efghi"

// run for string one and then two
const op1 = findDistinctChars(s1, s2)
const op2 = findDistinctChars(s2, s1)

// output results
console.log(op1)
console.log(op2)

about 4 years ago · Santiago Trujillo 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