c# tutorials

Updated : Feb 19, 2020 in Articles

C# technical Interview programs

Question 1:

Implement the UniqueNames method. When passed two arrays of names, it will return an array containing the names that appear in either or both arrays. The returned array should have no duplicates.

For example, calling MergeNames.UniqueNames(new string[]{‘Ava’ni, ‘Enum’, ‘Odd’}, new string[]{‘Odd’, ‘Sophia’, ‘Enum’}) should return an array containing Ava, Emma, Olivia, and Sophia in any order.

Answer 1:

the given example implemented using existing extension methods

public static void Main(string[] args)
        {
            string[] names1 = new string[] { "Ava", "Emma", "Olivia" };
            string[] names2 = new string[] { "Olivia", "Sophia", "Emma" };
            Console.WriteLine(string.Join(", ", names1.Concat(names2).Distinct().ToArray())); // should print Ava, Emma, Olivia, Sophia
        }

Answer 2:

 public static string[] UniqueNames(this string[] names1, string[] names2)
        {
            Dictionary<string, int> keyvale = new Dictionary<string, int>();
            string[] val = null;
            int i = 0;
            for (i = 0; i < names1.Length; i++)
            {
                keyvale.Add(names1[i], i);
            }
            for (int j = 0; j < names2.Length; j++)
            {
                if (keyvale.ContainsKey(names2[j]))
                {

                }
                else
                {
                    keyvale.Add(names2[j], i);
                }
            }
            val = keyvale.Select(z => z.Key).ToArray();
            return val;
        }

        public static void Main(string[] args)
        {
            string[] names1 = new string[] { "Ava", "Emma", "Olivia" };
            string[] names2 = new string[] { "Olivia", "Sophia", "Emma" };
            Console.WriteLine(string.Join(", ", GFG.UniqueNames(names1, names2))); // should print Ava, Emma, Olivia, Sophia
        }

Question 2:

how to find the “for” the below string?

int count = 0;
            string str = "hello man data for the distrub data for";
            string[] spltstr = str.Split(' ').ToArray();
            foreach (var item in spltstr)
            {
                if (item == "for")
                {
                    count++;
                }
            }
            Console.WriteLine(count);

or

static void Main(string[] args)
        {
            string Word = "Program to find how many times the is used in a string test the";
            var Value = Word.Split(' ');
            Dictionary<string, int> RepeatedWordCount = new Dictionary<string, int>();
            for (int i = 0; i < Value.Length; i++)
            {
                if (RepeatedWordCount.ContainsKey(Value[i]))
                {
                    int value = RepeatedWordCount[Value[i]];
                    RepeatedWordCount[Value[i]] = value + 1;
                }
                else
                {
                    RepeatedWordCount.Add(Value[i], 1);
                }
            }
            Console.WriteLine();
            Console.WriteLine("------------------------------------");
            Console.WriteLine("Repeated words and counts");
            foreach (KeyValuePair<string, int> kvp in RepeatedWordCount)
            {
                if (kvp.Key == "the")
                {
                    Console.WriteLine(kvp.Key + " Counts are " + kvp.Value);
                }
            }
            Console.ReadKey();
        }

Question 3:

how to find the “f” letter count in the string?

int count = 0;
string str = "hello man soorry for the distrub soorry for";
char[] spltstr = str.ToCharArray();
foreach (var item in spltstr)
{
  if (item == 'f')
   {
     count++;
    }
 }
Console.WriteLine(count);

Question 4:

Find the most repeated number in an integer array. If multiple numbers are there having the same maximum count, Return the largest number among then.

static int mostFrequent(int[] arr, int n)
        {
            Array.Sort(arr);
            int max_count = 1, res = arr[0];
            int curr_count = 1;
             List<int> maxcountval = new List<int>();
            for (int i = 1; i < n; i++)
            {
                if (arr[i] == arr[i - 1])
                {
                    curr_count++;
                    maxcountval.Add(arr[i]);
                }
                else
                {
                    if (curr_count > max_count)
                    {
                        max_count = curr_count;
                        res = arr[i - 1];
                    }
                    curr_count = 1;
                }
            }

            if (curr_count > max_count)
            {
                max_count = curr_count;
                res = arr[n - 1];
            }
            return maxcountval.Max();
        }

        public static void Main()
        {
            int[] arr = { 1, 5, 2, 1, 3, 3, 3, 2, 2, 1, 4, 4, 4 };
            int n = arr.Length;
            Console.WriteLine(mostFrequent(arr, n));
        }

Latest posts by DuttaluruVijayakumar (see all)
Like
Like Love Haha Wow Sad Angry
291