Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to find which number is repeated

Tags:

c

in a array[10],there are numbers from 1 to 9 in the array and one of the number is repeated (repeated number is also between 1 and 9) how to find the repeated number without using the loop, and array can be transversed only once from top to bottom.

this is not homework, this was asked in interview

like image 280
Manu Avatar asked Dec 02 '22 03:12

Manu


1 Answers

The shortest answer has to be based on Vladimir's answer. There is no for loop, however it is also not expandable to variable size arrays. It is:

int repeated_number = array[9]+array[8]+array[7]+array[6]+array[5]
                     +array[4]+array[3]+array[2]+array[1]+array[0]-45;

Sweet and simple, answers the question. I think, the problem is that all the people answering this question are to used to writing good sensible code that can handle variable situations, but this is a short simple question so deserves a short simple answer.

like image 122
AlastairG Avatar answered Dec 04 '22 16:12

AlastairG