Is there not a standard STL algorithm?
A while loop with nested if else might be 1) slow, 2) shaky/flaky, 3) difficult to maintain.
e.g. arrays of length 1'000'000, exception handling etc.
It looks like reinvention of wheel. It means less time for 'bigger' stuff.
BTW what's a good rationale...