Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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
expand button
Transcribed Image Text: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
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education