Wednesday, July 28, 2010

Second largest element in an unsorted array

How will you find out second largest element in an unsorted array?

If you would like to do it in one pass, then have two variables as max, secondMax. Do compare all values with thest values. Initially assign, firstValue as max and secondValue as secondMax do compare them.

Now the next question is, will it scale if you have large stream of numbers and if you would like to find Top N from that large stream of numbers?

Have array withe size N and repeat the above steps.

No comments:

Post a Comment