Back
I want to write a program that removes duplicates from an array without using Set, HashSet or any others like iterators.
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]); }}}
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.
31k questions
32.8k answers
501 comments
693 users