get the first recurring character javascript

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.


    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.


    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.

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