Showing posts with label Interview Questions. Show all posts
Showing posts with label Interview Questions. Show all posts

Tuesday, March 26, 2013

How to remove duplicate elements from given array . We should not use any collections or collection APIs.

Input is {4,7,2,8,7,8,5,4,9}.

Output should be {4,7,2,8,5,9}. Order is not important. Write an efficient program.


Answer is -

Algorithm:

1. Start from first element in the array. Initialize x=0;
2. Compare with previous elements - if not same insert that element in the position x. Increment x. If any of the element is same as current one - do not process it and do not increment X. Move to next element.
3.  Do this until all elements are done.
4. We have first X elements which are not duplicate elements.