dynamically allocate array size of 10 integer check for the duplicate etntry, if present print the no of duplicate entry for every element remove the duplicate entry and reallocate the memory
public class Main {
public static void main(String[] args) {
int[] array = new int[10];
array[0] = 2;
array[1] = 5;
array[2] = 4;
array[3] = 1;
array[4] = 9;
array[5] = 5;
array[6] = 6;
array[7] = 2;
array[8] = 1;
array[9] = 1;
int count;
int newSize = 0;
for (int i = 0; i < array.length; i++) {
count = 0;
for (int j = 0; j < array.length; j++) {
if (array[i] == array[j] && i != j) {
count++;
}
}
if (count > 0) {
System.out.println("The element " + array[i] + " the number of duplicates " + count);
} else {
newSize++;
}
}
int[] tmp = new int[newSize];
int k = 0;
for (int i = 0; i < array.length; i++) {
count = 0;
for (int j = 0; j < array.length; j++) {
if (array[i] == array[j] && i != j) {
count++;
}
}
if (count == 0) {
tmp[k++] = array[i];
}
}
array = tmp;
}
}
Comments
Leave a comment