Thursday, June 18, 2020

Majority Elements in C#

A program was written by my friend by Tom he called these program majority elements in C#.

 I am currently accepting programming work inventory system, enrollment system, accounting system, payroll system, information system, website design and development using WordPress, IT projects, school and application development, programming projects, thesis, and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you.


My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.My mobile number here in the Philippines is 09173084360. My telephone number at home here in Bacolod City, Negros Occidental Philippines is +63 (034) 4335675.

Here in Bacolod City, Negros Occidental I also accepting computer repair, web development using WordPress, Computer Networking, and Arduino Project development at a very affordable price. My personal website is http://www.jakerpomperada.comMy programming website is http://www.jakerpomperada.blogspot.comI am also a book author you can purchase my books on computer programming and information technology in the following links below.https://www.unlimitedbooksph.com/

Program Listing

using System;
using System.Collections.Generic;
using System.Linq;

   public class ArrayAlgorithms
   {
     public static bool HasMajorityElement<T>(IEnumerable<T> elements,
ref T res)
       where T: IComparable
     {
       T candidate = default(T);
       int count = 0;
       int num_elems = 0;

       foreach(var elem in elements)
       {
         ++num_elems;
         if (count == 0)
         {
           candidate = elem;
         }
         else if (candidate.Equals(elem))
         {
           ++count;
         }
         else
         {
           --count;
         }
       }
       var elem_count = elements.LongCount<T>((elem) =>
candidate.Equals(elem));
       if (elem_count > num_elems / 2)
       {
         res = candidate;
         return true;
       }
       return false;
     }
   }
   class Program
   {
     static void Main(string[] args)
     {
       string[] names = {"Anna", "Lisa", "Anna"};
       string name = string.Empty;

       Console.WriteLine("Testing string[] names.");
       if (ArrayAlgorithms.HasMajorityElement<string>(names, ref name))
       {
         Console.WriteLine("Found majority element: {0}", name);
       }

       Console.WriteLine("Testing int[] numbers.");
       int[] numbers = {1,2,3,4,5,6};
       int res = 0;
       if (ArrayAlgorithms.HasMajorityElement<int>(numbers, ref res))
       {
         Console.WriteLine("Found majority element: {0}", res);
       }
       else
       {
         Console.WriteLine("Counldn't find majority element");
       }
       Console.ReadKey();
     }
   }

No comments:

Post a Comment