How to get intersection of two strings in Node.js

1 Answer

0 votes
function getIntersection(str1, str2) {
  const set1 = new Set(str1);
  const set2 = new Set(str2);
  const intersection = [...set1].filter(char => set2.has(char));
  
  return intersection.join('');
}

module.exports = getIntersection;


const string1 = "csharp";
const string2 = "python";

console.log("Intersection:", getIntersection(string1, string2));




/*
run:

Intersection: hp

*/

 



answered Jul 7, 2025 by avibootz
...