Skip to content
the poor coder

get the first recurring character javascript

Poor Man
Published
Reading time
2 min read
On this page

How to Get the First Recurring Character in JavaScript?#

Have you ever had a long string of text and needed to find the first recurring character in it using JavaScript? Well, I have, and it can be a tricky task. However, there are a few methods to accomplish this goal.

Method 1: Using a For Loop#

The first method involves using a for loop to iterate through each character in the string and check if it appears more than once. If it does, we return that character and break out of the loop.

JavaScript

    function firstRecurringChar(str) {
      for (let i = 0; i < str.length; i++) {
        for (let j = i + 1; j < str.length; j++) {
          if (str[i] === str[j]) {
            return str[i];
          }
        }
      }
      return null;
    }
    
    const testStr = "hello world";
    const result = firstRecurringChar(testStr);
    console.log(result); // l
  

In the above code, we define a function called firstRecurringChar that takes in a string as an argument. We then use two nested for loops to compare each character in the string with all other characters to check whether they match or not. If we find a match, we return the character and break out of the loop. If we don't find any recurring characters, we return null. We then test our function by passing in a test string and logging the result to the console.

Method 2: Using a Hash Map#

The second method involves using a hash map to store each character in the string and its frequency. We loop through the string and for each character, we check if it exists in the hash map. If it does, we return that character as it is the first recurring character. If we reach the end of the loop without finding any recurring characters, we return null.

JavaScript

    function firstRecurringChar(str) {
      const charMap = {};
      for (let i = 0; i < str.length; i++) {
        if (charMap[str[i]]) {
          return str[i];
        } else {
          charMap[str[i]] = 1;
        }
      }
      return null;
    }
    
    const testStr = "hello world";
    const result = firstRecurringChar(testStr);
    console.log(result); // l
  

In the above code, we define a function called firstRecurringChar that takes in a string as an argument. We then define an empty hash map called charMap. We loop through the string and for each character, we check if it exists in the hash map. If it does, we return that character as it is the first recurring character. If it doesn't exist, we add it to the hash map with a value of 1. We then test our function by passing in a test string and logging the result to the console.

Conclusion#

Both methods achieve the same goal of finding the first recurring character in a string using JavaScript. The first method is more straightforward and relies on nested for loops to compare each character in the string with all other characters. The second method is more efficient and relies on a hash map to store each character and its frequency. Both methods have their advantages and disadvantages, but the choice ultimately depends on the specific use case and the size of the string being evaluated.

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