Find Repeating number Algorithm

By   Tewodros   Date Posted: Oct. 3, 2021  Hits: 851   Category:  Algorithm   Total Comment: 0             A+ A-


side

Find the number that is repeating from distinct random numbers.

Example:  3, 4, 8, 6, 7, 9, 4, 77  Ans = 4

Solution:

This problem can easily be solved by using hashtable

We put the elements of the array in hashtable and check if we already put the number if so we will return the number

           Hashtable ht = new Hashtable();

           for (int i = 0; i < a.Length; i++)      {

               if (!ht.ContainsKey(a[i]))

                   ht.Add(a[i],0);

               else           

                   return a[i];                       

           }


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* aatqan

Back to Top