Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!