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. 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