diff --git a/The First Non Repeated Character In A String b/The First Non Repeated Character In A String new file mode 100644 index 0000000..08dff2e --- /dev/null +++ b/The First Non Repeated Character In A String @@ -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 +}