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);

        }


Share this

Related Posts

Previous
Next Post »