0
public static int[] removeSameNumber(int[] input) {
   
   removeSamenumber() takes numbers (int) to array and returns new array with deleted duplicates.
   
   new int[] {2, 2} ==== new int[] {2}
   
   new int[] {1, 2, 1, 3, 2} ==== new int[] {1, 2, 3}

List, Set or other dynamic collections must not be used. You only have to deal with arrays.

Existing functions such as Arrays.copyOf () must not be used.

I tried a lot of different things but none of them is working

Im beginner and your help would very help me out :)

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • If you really don't want to use collections, you could iterate over the array, copying the values that had not already been encountered in a new array. But again, why wouldn't you want to use collections for this? They would make the job much simpler. – NatMath Feb 18 '21 at 18:39
  • What have you tried already? Would be interesting to see this already – Yassin Hajaj Feb 18 '21 at 18:47

2 Answers2

0

if you want to deduplicate an array without a library methods, you can do it so:

public static void main(String[] args) {
    int[] source = new int[] {1, 2, 1, 3, 2, 1, 4, 3, 2, 1, 11};
    int maxElement = 0;
    int sizeWithoutDouble = source.length;
    //get max int from source array, for getting temp array size
    for (int i = 0; i < source.length; i ++){
        maxElement = Math.max(maxElement, source[i]);
    }
    int temp[] = new int[maxElement + 1];
    //put in temp array uniuque int and decrement total int count
    for (int i = 0; i < source.length; i ++){
        if (temp[source[i]] == 0){
            temp[source[i]] = source[i];
        }else {
            sizeWithoutDouble--;
        }
    }
    //declare result array with size uniuque int
    int result[] = new int[sizeWithoutDouble];
    int currentRes = 0;
    //filling result array
    for (int i = 0; i < temp.length; i ++){
        if (temp[i] != 0){
            result[currentRes++] = temp[i];
        }
    }   
}

Output:

[1, 2, 3, 4, 11]
Dmitrii B
  • 2,672
  • 3
  • 5
  • 14
0
int[] temp = new int[inputs.length];
        int nUnique = 0;
        for(int i=0;i<inputs.length;i++) {
            boolean dup = false;
            for(int j=i+1;j<inputs.length;j++) {
                if(inputs[i]==inputs[j]) {
                    dup = true;
                    break;
                }
            }
            
            if(!dup) {
                temp[nUnique] = inputs[i];
                nUnique++;
            }
        }
        int[] result = new int[nUnique];
        for(int i=0;i<nUnique;i++) {
            result[i] = temp[i];
            //System.out.println(result[i]);
        }
return result;
Akash garg
  • 125
  • 6