-1

The code requires to delete any duplicate integers inside the array but keeping it simple. Without system.arraycopy or anything complicated, can't use set or anything "beyond" loops in knowledge. It's a constructor that receives an array of numbers, should check if the numbers duplicate and create an array without duplicates

For example:

{1,2,5,5,2,4,5,1} Would be {1,2,5,4} So it should create an array with the size of the numbers not duplicated.

Tried to create a loop that counts if the number repeats itself and only if not then it will add it but it's not the best idea since it won't count numbers that are being repeated at least once

int repeat = 0;enter code here
        int counter = 0;
        if(se.length < 2)
        {
            System.out.println("Array smaller than 2, please change the size!");
            return;
        }
        for(int i = 0; i < se.length; i++)
        {
            for(int j = 0; j < se.length; j++)
                if(se[i] == se[j])
                {
                    repeat++;
                }
            if(repeat == 0)
                counter++;
            repeat = 0;
        }
  • 1
    Why a constructor instead of a regular method? And are you asking us to do your work for you? That's not how this site works. – shmosel Jan 29 '19 at 00:36
  • Cause it was what was requested. I basically thought of creating a counter to see if the number repeats and if not then it should count it but it just gets me confused. – Alen Komras Jan 29 '19 at 00:39
  • I'll just update the post with what I've tried doing – Alen Komras Jan 29 '19 at 00:40
  • Also @shmosel , I just registered to the site, so I have no clue how stuff works around here :^) – Alen Komras Jan 29 '19 at 00:44
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. StackOverflow is not a design, coding, research, or tutorial service. https://stackoverflow.com/help/how-to-ask – NotZack Jan 29 '19 at 00:47
  • 1
    Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please read about [How to Ask a Question](https://stackoverflow.com/help/how-to-ask) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) to learn how to write effective questions. – Teocci Jan 29 '19 at 00:53
  • 2
    Possible duplicate of [Remove duplicates from integer array](https://stackoverflow.com/questions/13912004/remove-duplicates-from-integer-array) – Teocci Jan 29 '19 at 00:55

2 Answers2

1

You can try the following:

public static void removedups(int se[]) {
    int repeat = 0;
    int counter = 0;
    if(se.length < 2) {
        System.out.println("Array smaller than 2, please change the size!");
        return;
    }
    //Find out all the duplicates and replace with 0
    boolean isZeroPresent = false;
    int zeroindex = -1;
    for(int i = 0; i < se.length; i++) {
        for(int j = i + 1; j < se.length; j++) {
            if(se[i] == se[j]) {
                if(se[i] == 0 && !isZeroPresent) {
                    isZeroPresent = true;
                    zeroindex = i;
                }
                se[j] = 0;
            }
        }
    }

    //find the exact count of the array which does not contains duplicates
    int customIndex = 0;
    for(int i = 0; i < se.length; i++) {
        System.out.println(se[i]);
        if(isZeroPresent && zeroindex == i) {
            customIndex++;
            continue;
        }
        if(se[i] == 0) {
            continue;
        }
        customIndex++;
    }

    //create new array which will hold all the unique values and return 
    int arr[] = new int[customIndex];
    int j = 0;
    for(int i = 0; i < customIndex; i++) {
        if(se[i] == 0) {
            if(zeroindex == i) {
                arr[j] = se[i];
                j++;
            }
            continue;
        }
        arr[j] = se[i];
        j++;
    }
    System.out.println("-----------------");
    printArr(arr);
}

public static void printArr(int arr[]) {
    for(int i = 0; i < arr.length; i++) {
        System.out.println(arr[i]);
    }
}
aBnormaLz
  • 761
  • 5
  • 20
0

The trick is to have two indices, one to read from (i), and one result index (writingI) of the array part keeping the unique elements.

int[] removeDuplicates(int[] a) {
    int writingI = 0;
    // Invariant condition:
    //     writingI <= i
    //     a[0 <= j < writingI] are all unique
    for (int i = 0; i < a.length; ++i) {
        int n = a[i];
        boolean found = false;
        for (int j = 0; j < writingI; ++j) {
            if (a[j] == n) {
                found = true;
                break;
            }
        }
        if (!found) {
            a[writingI] = n;
            ++writingI;
        }
    }
    //return Arrays.copyOf(a, 0, writingI);
    int[] result = new int[writingI];
    for (int j = 0; j < writingI; ++j) {
        result[j] = a[j];
    }
    return result;
}

I have a bad conscience of showing you how the trick works. It really helps digging into a problem and finding a solution oneself.

Joop Eggen
  • 102,262
  • 7
  • 78
  • 129