-4

I'm trying to remove duplicate numbers from an array using a method but, unfortunately I can not solve it. This what I have done so far:

//method code
public static int[] removeDuplicates(int[] input){
    int []r=new int[input.length];

    for (int i = 0; i < input.length; i++) {
        for (int j = 0; j < input.length; j++) {
            if ((input[i]==input[j]) && (i != j)) {
                return r;
            }
        }
    }
    return r;
}
6
  • 1
    You are looking for a Set and the contains method. Commented May 20, 2013 at 23:06
  • 1
    do you know how to add elements to an array? Commented May 20, 2013 at 23:10
  • Do you want to create your own method to remove duplicates? Otherwise as MichaelIT said you can use set, where set dont allow duplicate items. Commented May 20, 2013 at 23:11
  • possible duplicate of Java Remove Duplicates from an Array? among numerous others Commented May 20, 2013 at 23:13
  • no, actually ineed to solve it without 'set' Commented May 20, 2013 at 23:18

3 Answers 3

1

The easiest thing to do is add all elements in a Set.

public static int[] removeDuplicates(int[] input){
    Set<Integer> set = new HashSet<Integer>();
    for (int i = 0; i < input.length; i++) {
        set.add(input[i]);
    }
    //by adding all elements in the Set, the duplicates where removed.
    int[] array = new int[set.size()];
    int i = 0;
    for (Integer num : set) {
        array[i++] = num;           
    }
    return array;
}
Sign up to request clarification or add additional context in comments.

6 Comments

This will change the order of elements, which may or may not be a problem.
thanks @nakosspy but i need to solve it without set what other way
Is it a school assignment?
@Thilo I think that there's a mistake there. Instead of if(isExists(result, i)) it must be if(!isExists(result, i)). Can you edit it?
@nakosspy: Looks like it. Also, the result array should probably be shortened. It's not my answer, so I don't want to just edit it. And it would be better for B'Tasneem to figure out the solution by himself. You should put a comment there.
|
1

You may do it this way:

public static int[] removeDuplicates(int[] input){
    boolean[] duplicate = new boolean[input.length];
    int dups = 0;
    for (int i = 0; i < input.length; i++) {
        if(duplicate[i])
            continue;
        for (int j = i + 1; j < input.length; j++) {
            if ((input[i]==input[j])) {
                duplicate[j] = true; // j is duplicate
                ++dups;
            }
        }
    }
    int[] r = new int[input.length] - dups;
    int index = 0;
    for(int i = 0; i < input.length; ++i)
        r[index++] = input[i];
    return r;
}

It can also be done in O(n log n). C++ code

Comments

0

If you don't want duplicates in your collection, well you shouldn't be using an Array in the first place. Use a set instead and you will never duplicates to remove in the first place.

If you only "sometimes" want no duplicates, well you better explain your situation further.

3 Comments

"If you don't want duplicates in your collection, well you shouldn't be using an Array in the first place". That is like saying "if you need command line arguments to be integers, you should not use main(String[] argv)". There are such things as input data, and it does not always come in the form most convenient to the program.
true, there is sometimes input data in a format you cannot constrain. But these situations are rarer than your think, and should be avoided whenever possible.
Same goes for output constraints.

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.