From 0a48055151dadbb81d900bbc77371004d7d349b9 Mon Sep 17 00:00:00 2001 From: igruiz91 <47140763+igruiz91@users.noreply.github.com> Date: Mon, 14 Oct 2019 00:02:40 +0200 Subject: [PATCH] The First Non Repeated Character In A String The First Non Repeated Character In A String --- The First Non Repeated Character In A String | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 The First Non Repeated Character In A String 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 +}