Skip to content
the poor coder

find common characters in two strings javascript

Poor Man
Published
Reading time
3 min read
On this page

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:

Text

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:

Text

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:

Text

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC