find common characters in two strings javascript

Find Common Characters in Two Strings Javascript

As a web developer, it's common to come across situations where you need to compare two strings and find the common characters in them. In Javascript, there are several ways to achieve this. Let's explore some of them:

Method 1: Using a for loop

One of the simplest ways to find common characters in two strings is to use a for loop. Here's how you can do it:


function findCommonChars(str1, str2) {
  var commonChars = "";
  for (var i = 0; i < str1.length; i++) {
    if (str2.indexOf(str1[i]) !== -1 && commonChars.indexOf(str1[i]) === -1) {
      commonChars += str1[i];
    }
  }
  return commonChars;
}

var str1 = "hello";
var str2 = "world";
var commonChars = findCommonChars(str1, str2);
console.log(commonChars); // Output: "lo"

In the above code, we are iterating through each character of the first string using a for loop. Inside the loop, we are checking if the character exists in the second string using the indexOf() method. If it exists and the character is not already present in the commonChars variable, we add it to the variable. Finally, we return the common characters.

Method 2: Using an object

Another approach to find common characters is by using an object. Here's how you can do it:


function findCommonChars(str1, str2) {
  var charMap = {};
  var commonChars = "";
  
  for (var i = 0; i < str1.length; i++) {
    if (!charMap[str1[i]]) {
      charMap[str1[i]] = 1;
    }
  }
  
  for (var j = 0; j < str2.length; j++) {
    if (charMap[str2[j]] && commonChars.indexOf(str2[j]) === -1) {
      commonChars += str2[j];
    }
  }
  
  return commonChars;
}

var str1 = "hello";
var str2 = "world";
var commonChars = findCommonChars(str1, str2);
console.log(commonChars); // Output: "lo"

In this method, we are creating an empty object charMap and iterating through the first string. For each character in the string, we are adding a key to the charMap object with the character as the key and the value as 1. This will create a map of all the characters in the first string. Then we iterate through the second string and check if the character exists in the charMap object. If it exists and the character is not already present in the commonChars variable, we add it to the variable. Finally, we return the common characters.

Method 3: Using Sets

With ES6, we can also use sets to find common characters in two strings. Here's how you can do it:


function findCommonChars(str1, str2) {
  var set1 = new Set(str1);
  var set2 = new Set(str2);
  var intersection = new Set([...set1].filter(char => set2.has(char)));
  return Array.from(intersection).join("");
}

var str1 = "hello";
var str2 = "world";
var commonChars = findCommonChars(str1, str2);
console.log(commonChars); // Output: "lo"

In this method, we are creating two sets set1 and set2 from the two strings. Then we create a new set intersection by filtering set1 with the has() method of set2. This will give us a set of all the common characters in the two strings. Finally, we convert the set to an array using Array.from() and join the elements to get the common characters as a string.

Conclusion

These are some of the ways you can find common characters in two strings using Javascript. Depending on your requirements, you can choose the method that suits you best.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe