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

java program For the partial implementation of the class IntArrayList below, imp

ID: 3849766 • Letter: J

Question

java program

For the partial implementation of the class IntArrayList below, implement the method removeDuplicates.

• After a call to the method removeDuplicates, the array designated by the instance variable elems contains no duplicated values.

• The method keeps the leftmost value for each duplicated pairs.

• Finally, the size of the array designated by elems must be exactly that of the number of unique values.

The execution of the method IntArrayList.test prints the following:

[2, 1, 1, 5, 3, 1, 2, 4]

[2, 1, 5, 3, 4]

public class Int Array List private int ll elem public Int Array List int ll elems precondition elem s is not null this elems J new int elems length l; System array copy (elems 0, this elems 0, elems length public void removeDuplicates

Explanation / Answer

Required function:

public void removeDuplicates() {
       Set<Integer> hashset = new LinkedHashSet<Integer>();
      
        /* Adding Array elements to the LinkedHashSet
         * in order to remove the duplicate elements and
         * to preserve the insertion order.
         */
       for(int i =0 ; i < elems.length;i++) {
        hashset.add(elems[i]);
       }
       /*
       * Adding all elements of LinkedHashSet to elems
       * as it contains no duplicate
       */
       elems = new int[hashset.size()];
            int i = 0;
            for (Integer num : hashset) {
                elems[i++] = num;         
            }
   }

Output checked:

[2, 1, 1, 5, 3, 1, 2, 4]
[2, 1, 5, 3, 4]