I was asked this question recently in an interview :
What is the most efficient way to find a repeated number in a sorted array?
My answer was based on using a hash table with key as array element and number of repetitions in array as value; iterate the array and update hash table. In the end, hash table can be checked for elements with count > 1 ; those are the repeated elements.
Is there a better way to do this ?
Thanks.
Well, you can do this with space of
O(1). Since its a sorted array, all you need to do it to subtract the current number with the next one. If the result is0then you have a repeated number.