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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With