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

Question 2: ( java code) please answer this as soon as possible - write comments

ID: 3910232 • Letter: Q

Question

Question 2: ( java code) please answer this as soon as possible - write comments in program

Complete the following Java method that accepts an ArrayList of Integer objects. It should return a new copy of the list with all adjacent duplicate values removed; e.g.

1 1 1 3 7 4 4 2 12 8 8 9 1 15 1 -6 -6

Becomes:

1 3 7 4 2 12 8 9 1 15 1 -6

Return the duplicate-free list. Do not modify the original list in any way. You may assume that the list contains only Integer objects.

public static ArrayList removeDuplicate(ArrayList list) { .... }

Explanation / Answer

here is your program : -------------->>>>>>>>>

import java.util.ArrayList;

public class ArrayManipulation{

public static ArrayList removeDuplicate(ArrayList list){
  ArrayList temp = new ArrayList();
  int ind = 0;
  for(int i = 0;i<list.size();i++){
   if(ind == 0){
    temp.add(list.get(i));
    ind++;
   }
   if(!temp.get(ind-1).equals(list.get(i))){
    temp.add(list.get(i));
    ind++;
   }
  }
  return temp;
}

public static void main(String[] args) {
  ArrayList list = new ArrayList();
  list.add(1);
  list.add(1);
  list.add(1);
  list.add(3);
  list.add(7);
  list.add(4);
  list.add(4);
  list.add(2);
  list.add(12);
  list.add(8);
  list.add(8);
  list.add(9);
  list.add(1);
  list.add(15);
  list.add(1);
  list.add(-6);
  list.add(-6);
  ArrayList ll = removeDuplicate(list);
  System.out.println(list+" "+ll);
}
}

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