Sunday 17 September 2017

In an array 1-100 multiple numbers are duplicates, how do you find it?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace TestAlgo
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayCls objcls = new ArrayCls();
            objcls.FindDuplicateNo();
}
}

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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace TestAlgo
{
   public class ArrayCls
    {
       


       public void FindDuplicateNo()
       {
           int[] intArr = new int[] { 6,5,2,9,2};

           Hashtable hash = new Hashtable();

           for (int i = 0; i < intArr.Count(); i++)
           {
               int counter=1;
               if (hash.ContainsKey(intArr[i]))
               {
                   int c =Convert.ToInt32( hash[intArr[i]]);
                   hash[intArr[i]] = c + 1;
               }
               else
               hash.Add(intArr[i],counter);
               
           }

           ICollection k = hash.Keys;
           foreach(int j in k)
           {
               if (Convert.ToInt32(hash[j]) > 1)
                   Console.WriteLine(j);
           }

           //hash.Add();
           
       
       }
    }
}

No comments:

Post a Comment

Recent Post

Parallel Task in .Net 4.0