Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (7k points)

I want to write a program that removes duplicates from an array without using Set, HashSet or any others like iterators.

1 Answer

0 votes
by (13.1k points)

You can do it like this:

import java.util.Arrays;

public class Practice {

public static void main(String[] args) {

    int a[] = { 1, 3, 3, 4, 2, 1, 5, 6, 7, 7, 8, 10 };

    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];

    for (int i = 0; i <= j; i++) {

        System.out.println(a[i]);

    }

}

}

Want to learn Java? Check out the Java certification from Intellipaat. 

Related questions

0 votes
1 answer
asked Feb 20, 2021 in Java by Jake (7k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...