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'
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)