RSS

How will you find the number repeated twice from an array of 1 to 100

Fri, Mar 5, 2010

Algorithm

Method 1) HashMap, O(n) time and space complexity
 
Declare a Hash Map
where
Key ————>Number/element/data
and
Data————>Number Of occurrence
Once all the elements are processed, then display the element whose number of occurrence is 2
 
 

Method 2) Binary Tree
 
Declare Binary tree
Visit all the element and insert into the tree
If any of the number is already exist then display the element which is the data with number of occurrence with 2
 
 

Method 3) Maths
 
Traverse the list once and find the sum of all numbers and store it in a.
Calculate b = n (n-1)/2 where n=99

Sharing ~ Helping Other:
  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • DZone
  • Slashdot
  • YahooMyWeb
  • StumbleUpon
  • Live
  • IndianPad
  • DotNetKicks
  • Technorati

Other Posts:

This post was written by:

eXclusiveMinds - who has written 500 posts on eXclusiveMinds.


Contact the author

Leave a Reply

You must be logged in to post a comment.