C# – String: Coding Interview Questions

1. Remove all the duplicate characters from a given string.

Method 1 : Loop through the given string using foreach. This will give you a single character per iteration. The use build-in C# String function IndexOf(). This will return -1 if the character is not found in the string variable called result. If not found then add this character into the result string. So in this way, we will add any character only once in the string.

 public static string RemoveDuplicateChars(string str)
 {
            string result = "";
            foreach (char ch in str)
            {
                if (result.IndexOf(ch) == -1)
                {
                    result += ch;
                }
            }
            return result;
  }

Method 2: Using LINQ

 public static string RemoveDuplicateCharsByLinq(string str)
 {
      return new string(str.ToCharArray().Distinct().ToArray());
 }

Method 3: Using HashSet
HashSet<T> is a collection of unique elements. It prevents duplicate elements to be inserted into the collection. Its performance is faster than the list.

 public static string RemoveDuplicateCharsByHashset(string str)
 {
      var unique = new HashSet<char>(str);
      return new string(unique.ToArray());
 }

2. Check whether 2 Strings are Anagram or not.

Two strings are Anagram if they contain the same set of characters, but the order of characters can be different. For Ex: Listen and Silent.

Method 1: Using Sorting (Time Complexity: O(nLogn))

        public static bool AreStringsAnagramsUsingSorting(string str1, string str2)
        {
            // If length of both strings is not same, then they cannot be anagram 
            if (str1.Length != str2.Length)
            {
                return false;
            }

            char[] ch1 = str1.ToLower().ToCharArray();
            char[] ch2 = str2.ToLower().ToCharArray();

            Array.Sort(ch1);
            Array.Sort(ch2);

            string val1 = new string(ch1);
            string val2 = new string(ch2);

            return val1 == val2;
        }

Method 2: Count Characters using One Array
Time Complexity: O(n)

        public static bool AreStringsAnagramsUsingCountChars(string str1, string str2)
        {
            if (str1.Length != str2.Length)
                return false;

            // Create a count array and initialize all values as 0 
            int[] count = Enumerable.Repeat(0, 256).ToArray();
            int i;

            for (i = 0; i < str1.Length; i++)
            {
                count[str1[i]]++;
                count[str2[i]]--;
            }

            for (i = 0; i < 256; i++)
                if (count[i] != 0)
                    return false;
            return true;
        }

Method 3: Bit Manipulation
Time Complexity: O(n)
Space Complexity: O(1)

       public static bool AreStringsAnagramsUsingXor(string str1, string str2)
        {
            if (string.IsNullOrWhiteSpace(str1) || string.IsNullOrWhiteSpace(str2))
                return false;

            if (str1.Length != str2.Length)
                return false;

            char[] string1CharArray = str1.ToLower().ToCharArray();
            char[] string2CharArray = str2.ToLower().ToCharArray();

            int xORValue = 0;

            for (int i = 0; i < string1CharArray.Length; i++)
            {
                xORValue ^= string1CharArray[i] ^ string2CharArray[i];
            }

            if (xORValue == 0)
                return true;

            return false;
        }

Written by 

Leave a Reply

Your email address will not be published. Required fields are marked *