Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java application containing the method that takes an array of integers a

ID: 3930108 • Letter: W

Question

Write a Java application containing the method that takes an array of integers and removes duplicates from this array (if any). For example, if this method gets the following array 1 4 9 16 9 7 4 9 11 then it should modify it to 1 4 9 16 7 11 The output of the program must be the original array (with duplicates) and the resulting one (with duplicates removed). Your method must work not only with the array above, but with ANY array of length at most 50. The program should load the array from the command-line parameters.

Explanation / Answer

Please let me know if you need more information:-

=====================================================

You can take any one of the below two program samples to remove duplicates:-

=========================================================

public class RemoveDuplicates {
   static void removeDuplicates(int[] a) {
       System.out.println("Array Original : ");
       for (int i = 0; i < a.length; i++) {
           System.out.print(a[i] + " ");
       }

       int uEle = a.length;

       for (int i = 0; i < uEle; i++) {
           for (int j = i + 1; j < uEle; j++) {
               if (a[i] == a[j]) {
                   a[j] = a[uEle - 1];

                   uEle--;

                   j--;
               }
           }
       }

       int[] arrayWithoutDuplicates = java.util.Arrays.copyOf(a, uEle);

       System.out.println();

       System.out.println("Array Without Duplicates : ");

       for (int i = 0; i < arrayWithoutDuplicates.length; i++) {
           System.out.print(arrayWithoutDuplicates[i] + " ");
       }
       System.out.println();
   }

   public static void main(String a[]) {
       int inputs[] = new int[a.length];
       for (int i = 0; i < a.length; i++) {
           inputs[i] = Integer.parseInt(a[i]);
       }
       removeDuplicates(inputs);
   }

}

-------------------------------------------------------------

public class RemoveDuplicates {

   int output[] = null;

   public void removeDuplicates(int[] a) {
       int uEle = a.length;

       for (int i = 0; i < uEle; i++) {
           for (int j = i + 1; j < uEle; j++) {
               if (a[i] == a[j]) {
                   a[j] = a[uEle - 1];

                   uEle--;

                   j--;
               }
           }
       }

       output = java.util.Arrays.copyOf(a, uEle);

   }

   public static void main(String a[]) {
       int inputs[] = new int[a.length];
       for (int i = 0; i < a.length; i++) {
           inputs[i] = Integer.parseInt(a[i]);
       }

       System.out.println("Array Original : ");
       for (int i = 0; i < a.length; i++) {
           System.out.print(a[i] + " ");
       }
       RemoveDuplicates rd = new RemoveDuplicates();
       rd.removeDuplicates(inputs);

       System.out.println();

       System.out.println("Array Without Duplicates : ");

       for (int i = 0; i < rd.output.length; i++) {
           System.out.print(rd.output[i] + " ");
       }
       System.out.println();
   }

}


================================================

SAMPLE_OUTPUT:-

===================================================

Array Original :
1   4   9   16   9   7   4   9   11  
Array Without Duplicates :
1   4   9   16   11   7  


===============================

Array Original :
1   4   9   16   9   7   4   9   11   87   65   4   3   16   23   56   43   8   7   4   3   2   8   7  
Array Without Duplicates :
1   4   9   16   3   7   43   2   11   87   65   8   23   56  

=====================================================

Thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote