DEV Community

DPC
DPC

Posted on

8 5 4 3 3

Daily JavaScript Challenge #JS-185: Find First Repeated Character in a String

Daily JavaScript Challenge: Find First Repeated Character in a String

Hey fellow developers! 👋 Welcome to today's JavaScript coding challenge. Let's keep those programming skills sharp!

The Challenge

Difficulty: Easy

Topic: String Manipulation

Description

Write a function that finds the first character in a given string that repeats compared to the characters that precede it. If there is no such character, return 'None'.

Ready to Begin?

https://www.dpcdev.com/

  1. Fork this challenge
  2. Write your solution
  3. Test it against the provided test cases
  4. Share your approach in the comments below!

Want to Learn More?

Check out the documentation about this topic here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

Join the Discussion!

  • How did you approach this problem?
  • Did you find any interesting edge cases?
  • What was your biggest learning from this challenge?

Let's learn together! Drop your thoughts and questions in the comments below. 👇


This is part of our Daily JavaScript Challenge series. Follow me for daily programming challenges and let's grow together! 🚀

javascript #programming #coding #dailycodingchallenge #webdev

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (2)

Collapse
 
seandinan profile image
Sean Dinan

I'm sure it could be better optimized, but I just took a simple iteration approach:

function firstRepeatedCharacter(str) {
  const split = str.split('');
  for (let i = 0; i < split.length; i++){
      const index = split.findIndex(char => char === split[i]);
      if (index !== undefined && index !== i){
          return split[i];
      }
  }
  return 'None';
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dotallio profile image
Dotallio

I always get tripped up by strings where no character repeats at all, like 'abc', so I make sure to test that early. Did anyone find a one-liner approach they liked?

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more