Datastructures and Algorithms in C#

Datastructures and Algorithms in C#

 

Datastructures and Algorithms in C# 


  1. C# Programming Questions on Strings
  2. C# Programming Questions on  Numbers in C#
  3. C# Programming Questions on Arrays in C#
  4. C# Programming Questions on Datastructures
  5. Problem Solving Interview Questions in C#
  6. Sorting Algorithms And other Algorithms in C#

----------------------------------------------------------------------------------------------

Finding sum of digits of a number until sum becomes single digit in C#

 

Finding sum of digits of a number until sum becomes single digit in C#

Find the Number Occurring Odd Number of Times in C#

 

Find the Number Occurring Odd Number of Times in C#


C# Code

        static void getOddOccurrence()

        {

            int[] arr = { 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2 };

            int length = arr.Length;

 

            for (int i = 0; i < length; i++)

            {

                int count = 0;

 

                for (int j = 0; j < length; j++)

                {

                    if (arr[i] == arr[j])

                        count++;

                }

                if (count % 2 != 0)

                {

                    Console.WriteLine("Odd Occurrence number is :- " + arr[i]);

                    return;

                }

            }

            Console.WriteLine(-1);

        }

output


Logic 2:-

    static void getOddOccurrence()

        {

            int[] arr = { 2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2 };

            int length = arr.Length;

            int res = 0;

            for (int i = 0; i < length; i++)

            {

                res = res ^ arr[i];

            }

         Console.WriteLine("Odd Occurrence number is :-  " +  res);

        }


Swap min and max element in integer array in C#

 

Swap min and max element in integer array in C#

C# Code

public static void MinMaxSwap()

        {

            int[] input = { 1, 2, 4, 5, 6, 10, 9 };

            if (input.Length == 0)

                return;

 

            int maxPos = 0;

            int minPos = 0;

            int valMax = 0;

            int valMin = 0;

 

            for (int i = 1; i < input.Length; i++)

            {

                if (input[maxPos] < input[i])

                    maxPos = i;

                if (input[minPos] > input[i])

                    minPos = i;

            }

            valMax = input[maxPos];

            valMin = input[minPos];

            input[maxPos] = valMin;

            input[minPos] = valMax;

 

            foreach (var item in input)

            {

                Console.WriteLine(item);

            }

        }



Output

Logic 2

C# Code

        public static void MinMaxSwap2()

        {

            int[] input = { 1, 2, 4, 5, 6, 10, 9 };

            int min = 0;

            int max = 0;

 

            for (int i = 1; i < input.Length; i++)

            {

                if (input[min] > input[i])

                    min = i;

                if (input[max] < input[i])

                    max = i;

            }

            int temp = input[min];

            input[min] = input[max];

            input[max] = temp;

 

            foreach (var item in input)

            {

                Console.WriteLine(item);

            }

        }


Output 


How to search a string in String array in C#

 

How to search a string in String array in C#


1. Using Array.Find()

  static void SearchInArray()

        {

            string[] names = { "DotNet", "Munesh Sharma", "Kishan", "Ravi", "Mohan", "Rahul", "Govind" };

            var stringToFind = "Rahul";

 

            var result = Array.Find(names, x => x == stringToFind);

 

            Console.WriteLine(result);

        }


Output



2. Search string which start from a character 

        static void SearchInArray()

        {

            string[] names = { "DotNet", "Munesh Sharma", "Kishan", "Ravi", "Mohan", "Rahul", "Govind" };

          

            var result = Array.Find(names, x => x.StartsWith("R"));

 

            Console.WriteLine(result[1]);

        }


the result will be Ravi and the reason is we are search string which start from R and we are using Find method ,hence giving first string it find.





if we want all the string which start from R then we have to use FindAll method like below

        static void SearchInArray()

        {

            string[] names = { "DotNet", "Munesh Sharma", "Kishan", "Ravi", "Mohan", "Rahul", "Govind" };

          

            var result = Array.FindAll(names, x => x.StartsWith("R"));

 

            foreach (var item in result)

            {

                Console.WriteLine(item);

            }

           

        }


Output



3. Search string by length

     static void SearchInArray()

        {

            string[] names = { "DotNet", "Munesh Sharma", "Kishan", "Ravi", "Mohan", "Rahul", "Govind" };

          

            var result = Array.FindAll(names, x => x.Length > 5);

 

            foreach (var item in result)

            {

                Console.WriteLine(item);

            }

           

        }


output



Count number of occurrences(or frequency) in a sorted array in C#

 

Count number of occurrences(or frequency) in a sorted array in C#


C# code

        static void Main(string[] args)

        {

            //   Count number of occurrences(or frequency) in a sorted array

            int[] arr = { 1, 2, 2, 2, 2, 3, 4, 7, 8, 8 };

            int n1 = arr.Length;

            Console.Write("Enter the Traget Number : ");

 

            int number = int.Parse(Console.ReadLine());

 

            Console.Write(countOccurrences(arr, n1, number));

 

 

            Console.ReadLine();

        }

        static int countOccurrences(int[] arr, int n, int x)

        {

            int res = 0;

 

            for (int i = 0; i < n; i++)

                if (x == arr[i])

                    res++;

 

            return res;

        }


output



Create Unique integer array in C# | check if array contains a duplicate number in C#

 

Create Unique array  in C# | check if array contains a duplicate number in C#


In this article we will see to create unique array.

Determine if any two integers in array sum to given integer in C#

 

Determine if any two integers in array sum to given integer