Finding and returning uncommon characters between two strings in JavaScript


Problem

We are required to write a JavaScript function that takes in two strings. Our function should return a new string of characters which is not common to both the strings.

Example

Following is the code −

 Live Demo

const str1 = "xyab";
const str2 = "xzca";
const findUncommon = (str1 = '', str2 = '') => {
   const res = [];
   for (let i = 0; i < str1.length; i++){
      if (!(str2.includes(str1[i]))){
         res.push(str1[i])
      }
   }
   for (let i = 0; i < str2.length; i++){
      if (!(str1.includes(str2[i]))){
         res.push(str2[i])
      }
   }
   return res.join("");
};
console.log(findUncommon(str1, str2));

Output

ybzc

Updated on: 20-Apr-2021

878 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements