The first time I write in SO, because he could not find a solution on his own. At the interview, I was given the task of writing a method that checks the characters in a string for a unique one.
Requirements: do not use LINQ . Preferred: do not use additional data types (dictionary, HashSet ... etc. Arrays and lists Valid )
Example:
"Hello" - return false; "Helo" - return true
My implementation:
static HashSet<char> charSet = new HashSet<char>(); static bool IsUniqueChar(string str) { foreach (char c in str) { charSet.Add(c); } return charSet.Count() == str.Length; }
But it does not meet the requirements of data types and is not the best performance ... I also tried a dictionary approach:
static Dictionary<char,bool> charSetDictionary = new Dictionary<char,bool>(); static bool IsUniqueChar(string str) { try { foreach (char c in str) { charSetDictionary.Add(c,true); } } catch { return false; }
But he is not better than the previous one. I will welcome any idea how best to solve this problem? ps
static void Main(string[] args) { Stopwatch sw = Stopwatch.StartNew(); IsUniqueChar("Hello"); sw.Stop(); Console.WriteLine("Elapsed={0}", sw.Elapsed);
string c # duplicates
user3311426
source share