Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions The First Non Repeated Character In A String
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*You need to write a function, that returns the first non-repeated character in the given string.

For example for string "test" function should return 'e'.
For string "teeter" function should return 'r'.

If a string contains all unique characters, then return just the first character of the string.
Example: for input "trend" function should return 't'

You can assume, that the input string has always non-zero length.

If there is no repeating character, return null in JS or Java, and None in Python.*/



function firstNonRepeated(s) {
for (let i = 0; i < s.length; i++) {
var c = s.charAt(i)
if (s.indexOf(c) == i && s.indexOf(c, i + 1) == -1) return c
}
return null
}