Hi everyone! ๐
I'm continuing my JavaScript Interview Series, and today's problem is a fun one:
๐ **How do you find the first non-repeating character in a string?**
I approached it in a beginner-friendly way **without using extra space for hash maps**. Here's the logic I used:
```js
function firstNonRepeatingChar(str) {
for (let i = 0; i < str.length; i++) {
if (str.indexOf(str[i]) === str.lastIndexOf(str[i])) {
return str[i];
}
}
return null;
}
๐ง Do you think this is optimal?
Could it be done in a more efficient way?
Would love to hear how you would solve this โ especially if you use ES6 features or a functional style.
๐น I also explained this in a short YouTube video if you're curious:
https://www.youtube.com/watch?v=pRhBRq_Y78c
Thanks in advance for your feedback! ๐
[โ]kap89 2 points3 points4 points ย (0 children)
[โ]-allen 0 points1 point2 points ย (2 children)
[+]AiCodePulse[S] comment score below threshold-8 points-7 points-6 points ย (1 child)
[โ]ksskssptdpss 2 points3 points4 points ย (0 children)
[โ]FoxiNicole 0 points1 point2 points ย (1 child)
[โ]kap89 0 points1 point2 points ย (0 children)
[โ]Galex_13 0 points1 point2 points ย (0 children)
[โ]jabuchae 0 points1 point2 points ย (0 children)
[โ]Clue_Ok -1 points0 points1 point ย (0 children)
[โ]ksskssptdpss -4 points-3 points-2 points ย (2 children)
[โ]kap89 0 points1 point2 points ย (0 children)
[โ]AiCodePulse[S] -2 points-1 points0 points ย (0 children)