1

As the header said. I want to delete all duplicate element from an array without using any library or collection. I usually use Set or HashMap but it is not possible anymore in this case. I also thought about sort the array and check from the beginning to the end like

If(arr[i]==a[i+1])
delete arr[i]; i--;

Or maybe something like using 2 for loops. But they are not efficient enough. Are there any other more efficiently way to delete duplicates? Thank you!

7
  • what's the element data type ? Commented Jan 11, 2021 at 8:21
  • Why is not possible to use a set? if the elements are comparable it should works Commented Jan 11, 2021 at 8:21
  • data type is only int Commented Jan 11, 2021 at 8:22
  • Aren't Set a collection? As the title said, I cannot use any Collection, including Set/HashMap Commented Jan 11, 2021 at 8:22
  • Can you use a Stream? Stream is not a collection and because of this is not in collection package Commented Jan 11, 2021 at 8:29

4 Answers 4

1

If we sort the array, any duplication between the values will be close to each other. That way we can remove them

  int a[] = { 1,9,55,1,8,9,77,2,5,54,7,10,11 };
    Arrays.sort(a);
    int j = 0;
    for (int i = 0; i < a.length - 1; i++) {
        if (a[i] != a[i + 1]) {
            a[j] = a[i];
            j++;
        }
    }
    a[j] = a[a.length - 1];
 return a;
Sign up to request clarification or add additional context in comments.

6 Comments

Yeah, I thought about that way too. But my teacher said it is not the most efficiently way
If you are looking for efficiency, without any shadow of a doubt you can use hash map
Aren't Hashmap a collection too?
Currently the efficiency is nLOg n Sorting may take nLog n, And the inner loop take o(n)
w3schools.com/java/java_hashmap.asp You must run on the loop once, store in the hash map as ey any value you found. If it exists in the hash map you will ignore the storage. Efficiency o (n)
|
1

If you can use Streams (they are not collections and can be used since Java 8), You can do something like this:

int[] result = Arrays.stream(a).distinct().toArray();

Comments

0

This is another possibility. The cost is O(n + m) where m is the max value of the input array.

    public static void main(String[] args) {
        int arr[] = {23, 56, 78, 92, 44, 3, 3, 3, 23, 11, 10, 10, 10, 10};
        // Find the size for the new array to allocate.
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        // Mark the values stored in the array (hit or miss array)
        int[] presence = new int[max + 1];
        for (int i = 0; i < arr.length; i++) {
            presence[arr[i]] = 1;
        }
        // Find the size for the new array to allocate
        int count = 0;
        for (int i = 0; i < presence.length; i++) {
            if (presence[i] != 0) {
                count++;
            }
        }
        // Store the element in the new array when hit
        int[] res = new int[count];
        int index = 0;
        for (int i = 0; i < presence.length; i++) {
            if (presence[i] != 0) {
                res[index] = i;
                index++;
            }
        }
        for (int i = 0; i < res.length; i++) {
            System.out.print(res[i] + ", ");
        }
    }

I know for sure this can improved significantly , but you may use this as a starting point.

2 Comments

What if array contains Integer.MAX_VALUE?
yep, there are several limits, another one could be with an input array of negative numbers. There are a lot of assumptions with this solution, because who posted the question has not been very clear on the constraints to this problem (the only constraint was a linear time solution since sorting and removing was not efficient enough according to the poster's teacher)
0

We can remove duplicates from the array using the code below without using any collections or streams. Have done it by taking the help of String class. Also the order will be preserved

   // Input Array
    int a[] = {1, 9, 55, 1, 8, 9, 77, 2, 5, 54, 7, 10, 11};

    String s = "";

    for (int i = 0; i < a.length; i++) {
        String s1 = String.valueOf(a[i]);
        if (!s.startsWith(s1 + " ") && !s.contains(" " + s1 + " ")) {
            s = s.concat(s1 + " ");
        }
    }

    System.out.println(s);
    // output: 1 9 55 8 77 2 5 54 7 10 11

Comments

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.