1

First, I know there is a lot of duplicate answers already, but I can't find what I want, even searched in Google. This is a question asked in an interview.

So, for my question: I have the next int array:

int[] array = {1, 1, 1, 2, 2, 3, 4, 4, 4, 5, 5, 6, 7, 7, 8, 8, 9, 9};

EDIT: You can assume that the array is sorted.

I want to get only the distinct values, without the duplicates, meaning:

array = {1, 2, 3, 4, 5, 6, 7, 8, 9, ......};

EDIT: Assume that you don't need to shrink the array, but return the values in their sorted order, and the rest of the values at the end.

There is a couple of instructions:

  1. Don't use any other or new array, meaning use the same array for returning the result.
  2. Don't use any Collections like Set or ArrayList.
  3. Make it the most useful you can.

Iv'e tried to do this with Set, but now I want something different. Also tried to replace the duplicate values with -1 value, but this is true only when I'm assuming that I'm using positive values only.

If you find identical question, tell me and I will delete this one.

Thanks.

7
  • A set is what you need to create from that data - which is what the Set class is for. Don't reinvent the wheel. Commented Sep 21, 2012 at 12:36
  • Are we to assume the input array will always be sorted? Commented Sep 21, 2012 at 12:38
  • You can't shrink an array so I don't know how you intend to do that. Can you assume the values are sorted already? Commented Sep 21, 2012 at 12:40
  • @Evan Mulawski This what the interviewer asked. Believe me, I was not trying to reinvent the wheel. Commented Sep 21, 2012 at 12:41
  • Actually, a sentence like the one in the second line of code means that a new array is used. In many languages, in fact, the modification of the size of such an array implies the usage of a new one. What are, therefore, the specific limitations of alternative structure usage? Commented Sep 21, 2012 at 12:42

1 Answer 1

9

If they're in order, that's not terribly difficult.

/**
 * removes duplicates in the provided sorted array
 * @return the number of different elements (they're at the beginning)
 */
public static int shrink(int[] array) {
    int w = 0;
    for (int i=0; i<array.length; i++) {
      if (i==0 || array[i]!=array[i-1]) {
          array[w++]=array[i];
      }
    }
    return w;
}

After that, only the first w elements are interesting.

Sign up to request clarification or add additional context in comments.

4 Comments

I edited in order to try to implement also requirement 3 (be the most useful possible).
@dystroy I think you can avoid i==0 in if statement if you write if(array[i]!=array[i+1])
@loler I don't think so. Test it with non duplicates at both ends.
@dystroy ok, i think then you have to avoid overload in case of last element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.