DEV Community

DPC
DPC

Posted on

8 6 4 5 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

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

Build seamlessly, securely, and flexibly with MongoDB Atlas. Try free.

MongoDB Atlas lets you build and run modern apps in 125+ regions across AWS, Azure, and Google Cloud. Multi-cloud clusters distribute data seamlessly and auto-failover between providers for high availability and flexibility. Start free!

Learn More

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?

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server ⏰

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay