-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;
}
SomeShinyObject
  • 7,581
  • 6
  • 39
  • 59
B'Tasneem
  • 13
  • 1
  • 5
  • 1
    You are looking for a [Set](http://docs.oracle.com/javase/6/docs/api/java/util/HashSet.html) and the `contains` method. –  May 20 '13 at 23:06
  • 1
    do you know how to add elements to an array? – mre May 20 '13 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. – Smit May 20 '13 at 23:11
  • possible duplicate of [Java Remove Duplicates from an Array?](http://stackoverflow.com/questions/10056729/java-remove-duplicates-from-an-array) among numerous others – Brian Roach May 20 '13 at 23:13
  • no, actually ineed to solve it without 'set' – B'Tasneem May 20 '13 at 23:18
  • possible duplicate of [Remove duplicates from integer array](http://stackoverflow.com/questions/13912004/remove-duplicates-from-integer-array) – Thilo May 20 '13 at 23:19

3 Answers3

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;
}
nakosspy
  • 3,904
  • 1
  • 26
  • 31
  • This will change the order of elements, which may or may not be a problem. – Thilo May 20 '13 at 23:20
  • thanks @nakosspy but i need to solve it without **set** what other way – B'Tasneem May 20 '13 at 23:24
  • Is it a school assignment? – nakosspy May 20 '13 at 23:25
  • @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 May 20 '13 at 23:40
  • @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. – Thilo May 20 '13 at 23:48
  • OK, I agree, B'Tasneem has enough info to figure it out himself. – nakosspy May 20 '13 at 23:51
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

Community
  • 1
  • 1
RiaD
  • 46,822
  • 11
  • 79
  • 123
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.

Ericson2314
  • 133
  • 7
  • 1
    "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. – Thilo May 21 '13 at 03:49
  • 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. – Ericson2314 May 23 '13 at 04:45
  • Same goes for output constraints. – Ericson2314 May 23 '13 at 04:53