In java, Implement a method that removes all duplicates. Keep the first instance of the element but remove all subsequent duplicate elements. public void removeDuplicates() see the picture for the right output. Given files: Demo6.java public class Demo6 { public static void main(String[] args) { MyLinkedList list = new MyLinkedList<>(); TestBench.addToList(list); TestBench.addToList(list); TestBench.addToList(list); TestBench.addToList(list); for (int x = 0; x < list.size(); x += 1 + x/3) { System.out.println("Removing element at index: " + x); list.remove(x); } System.out.println("Result"); System.out.println(list); System.out.println("Shuffling"); list.shuffle(7777); System.out.println(list); System.out.println("Removing duplicates (keeping first instance)"); list.removeDuplicates(); System.out.println(list); } } TestBench.java import java.util.AbstractList; public class TestBench { public static AbstractList buildList() { return addToList(new MyLinkedList<>()); } public static AbstractList addToList(AbstractList list) { String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (int x = 0; x < data.length(); x++) { list.add(data.charAt(x) + ""); } return list; } public static void test(AbstractList list) { System.out.println("--- Beginning Tests ---"); System.out.println("No changes"); System.out.println(list); System.out.println("Testing size()"); System.out.println(list.size()); System.out.println("Testing add(int index, E element)"); list.add(0, "AAA"); list.add(0, "BBB"); list.add(10, "CCC"); list.add(15, "DDD"); list.add(list.size() - 1, "EEE"); System.out.println(list); System.out.println("Testing get(int index)"); System.out.println("Element at 0: " + list.get(0)); System.out.println("Element at 10: " + list.get(10)); System.out.println("Element at 20: " + list.get(20)); System.out.println("Element at 26: " + list.get(26)); System.out.println("Element at last position: " + list.get(list.size() - 1)); System.out.println("Testing remove(int index)"); System.out.println(list.remove(0)); System.out.println(list.remove(0)); System.out.println(list.remove(0)); System.out.println(list.remove(10)); System.out.println(list.remove(20)); System.out.println(list.remove(list.size() - 1)); System.out.println(list); System.out.println("Testing set(int index, E element)"); list.set(0, "QQQ"); list.set(5, "WWW"); list.set(10, "EEE"); list.set(12, "RRR"); list.set(4, "TTT"); list.set(20, "TTT"); list.set(list.size() - 1, "YYY"); System.out.println(list); System.out.println("Testing indexOf(Object o)"); System.out.println("indexOf QQQ: " + list.indexOf("QQQ")); System.out.println("indexOf WWW: " + list.indexOf("WWW")); System.out.println("indexOf D: " + list.indexOf("D")); System.out.println("indexOf HELLO: " + list.indexOf("HELLO")); System.out.println("indexOf RRR: " + list.indexOf("RRR")); System.out.println("indexOf TTT: " + list.indexOf("TTT")); System.out.println("indexOf GOODBYE: " + list.indexOf("GOODBYE")); System.out.println("Testing lastIndexOf(Object o)"); System.out.println("lastIndexOf QQQ: " + list.lastIndexOf("QQQ")); System.out.println("lastIndexOf WWW: " + list.lastIndexOf("WWW")); System.out.println("lastIndexOf D: " + list.lastIndexOf("D")); System.out.println("lastIndexOf HELLO: " + list.lastIndexOf("HELLO")); System.out.println("lastIndexOf RRR: " + list.lastIndexOf("RRR")); System.out.println("lastIndexOf TTT: " + list.lastIndexOf("TTT")); System.out.println("lastIndexOf GOODBYE: " + list.lastIndexOf("GOODBYE")); System.out.println("Testing clear()"); list.clear(); System.out.println(list); System.out.println("Testing clear() [second time]"); list.clear(); System.out.println(list); System.out.println("Refilling the list"); addToList(list); System.out.println(list); System.out.println("--- Ending Tests ---"); } }
In java, Implement a method that removes all duplicates. Keep the first instance of the element but remove all subsequent duplicate elements.
public void removeDuplicates()
see the picture for the right output.
Given files:
Demo6.java
public class Demo6 {
public static void main(String[] args) {
MyLinkedList<String> list = new MyLinkedList<>();
TestBench.addToList(list);
TestBench.addToList(list);
TestBench.addToList(list);
TestBench.addToList(list);
for (int x = 0; x < list.size(); x += 1 + x/3) {
System.out.println("Removing element at index: " + x);
list.remove(x);
}
System.out.println("Result");
System.out.println(list);
System.out.println("Shuffling");
list.shuffle(7777);
System.out.println(list);
System.out.println("Removing duplicates (keeping first instance)");
list.removeDuplicates();
System.out.println(list);
}
}
TestBench.java
import java.util.AbstractList;
public class TestBench {
public static AbstractList<String> buildList() {
return addToList(new MyLinkedList<>());
}
public static AbstractList<String> addToList(AbstractList<String> list) {
String data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int x = 0; x < data.length(); x++) {
list.add(data.charAt(x) + "");
}
return list;
}
public static void test(AbstractList<String> list) {
System.out.println("--- Beginning Tests ---");
System.out.println("No changes");
System.out.println(list);
System.out.println("Testing size()");
System.out.println(list.size());
System.out.println("Testing add(int index, E element)");
list.add(0, "AAA");
list.add(0, "BBB");
list.add(10, "CCC");
list.add(15, "DDD");
list.add(list.size() - 1, "EEE");
System.out.println(list);
System.out.println("Testing get(int index)");
System.out.println("Element at 0: " + list.get(0));
System.out.println("Element at 10: " + list.get(10));
System.out.println("Element at 20: " + list.get(20));
System.out.println("Element at 26: " + list.get(26));
System.out.println("Element at last position: " + list.get(list.size() - 1));
System.out.println("Testing remove(int index)");
System.out.println(list.remove(0));
System.out.println(list.remove(0));
System.out.println(list.remove(0));
System.out.println(list.remove(10));
System.out.println(list.remove(20));
System.out.println(list.remove(list.size() - 1));
System.out.println(list);
System.out.println("Testing set(int index, E element)");
list.set(0, "QQQ");
list.set(5, "WWW");
list.set(10, "EEE");
list.set(12, "RRR");
list.set(4, "TTT");
list.set(20, "TTT");
list.set(list.size() - 1, "YYY");
System.out.println(list);
System.out.println("Testing indexOf(Object o)");
System.out.println("indexOf QQQ: " + list.indexOf("QQQ"));
System.out.println("indexOf WWW: " + list.indexOf("WWW"));
System.out.println("indexOf D: " + list.indexOf("D"));
System.out.println("indexOf HELLO: " + list.indexOf("HELLO"));
System.out.println("indexOf RRR: " + list.indexOf("RRR"));
System.out.println("indexOf TTT: " + list.indexOf("TTT"));
System.out.println("indexOf GOODBYE: " + list.indexOf("GOODBYE"));
System.out.println("Testing lastIndexOf(Object o)");
System.out.println("lastIndexOf QQQ: " + list.lastIndexOf("QQQ"));
System.out.println("lastIndexOf WWW: " + list.lastIndexOf("WWW"));
System.out.println("lastIndexOf D: " + list.lastIndexOf("D"));
System.out.println("lastIndexOf HELLO: " + list.lastIndexOf("HELLO"));
System.out.println("lastIndexOf RRR: " + list.lastIndexOf("RRR"));
System.out.println("lastIndexOf TTT: " + list.lastIndexOf("TTT"));
System.out.println("lastIndexOf GOODBYE: " + list.lastIndexOf("GOODBYE"));
System.out.println("Testing clear()");
list.clear();
System.out.println(list);
System.out.println("Testing clear() [second time]");
list.clear();
System.out.println(list);
System.out.println("Refilling the list");
addToList(list);
System.out.println(list);
System.out.println("--- Ending Tests ---");
}
}
![Test Case 1
Removing element at index: 0 \n
Removing element at index: 1
Removing element at index: 2 \n
Removing element at index: 3 \n
Removing element at index: 5
Removing element at index: 7 r
Removing element at index: 10 \n
Removing element at index: 14 \n
Removing element at index: 19 \n
Removing element at index: 26
Removing element at index: 35
Removing element at index: 47
Removing element at index: 63
Removing element at index: 85
Result n
[в, D, F, н, І, к, L, N, о, Р, R, S, т, U, w, х, Y, z, A, с, D, Е, F, G, н, I, к, L, м, N, о, р, 0, R, S, U, V, W, х, у, z, А, в, с, D, Е, F, н, I, 3, к, L, м, N, о, Р, Q. R, 5, т, U, V, w, Y, z, A, в, с, D, Е, F, G, н, I, J, к, L, м, N, о, Р, Q, R.
5, т, V, W, х, Ү, Z]|n
Shuffling n
[Q, L, к, W, с, о, R, F, м, в, F, S, z, н, D, т, V, R, T, G, N, L, т, о, Z, U, Y, S, V, z, 0, х, н, Ү, D, W, N, A, Х, D, Y, D, F, в, I, А, о, Р, А, Е, І, Р, Р, F, L, E, G, H, Мм, 3, в, Ј, с, к, к, м, S, N, н, R, С, U, I, E, Y, к, и, N, Р, Ss, w, V, L,
R, Z, Q, N, о, I, х]|m|
Removing duplicates (keeping first instance) \n
[о, L, к, м, с, R, F, М, в, S, Z, н, D, т, V, G, N, о, U, Y, х, А, I, Р, Е, J] m](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F4e8a92e6-ccd1-4b67-b436-479a3fd34a6c%2F351bb5d3-0be4-4e5a-9fa6-142d10bd3b2b%2Fp59rny_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 12 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)