Answer to Question #168859 in Java | JSP | JSF for Hanbun

Question #168859

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


1
Expert's answer
2021-03-05T01:10:27-0500
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;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog