Question

In: Computer Science

I need to implement a method that removes any duplicate items in a list and returns...

I need to implement a method that removes any duplicate items in a list and returns the new values   

private static linkedlist removeDuplicates(linkedlist list)
{

   return list;
}

Solutions

Expert Solution

Solution:

public class removeDuplicates

{

    static class element  

    {

        int val;

element next;

        public element(int val)

        {

            this.val = val;

        }

    }

    /* Function to remove duplicates from a unsorted linked list */

    static void removeDuplicate(element head)  

    {

        // Hash to store seen values

        HashSet<Integer> hs = new HashSet<>();

        //Pick elements one by one

        element current = head;

        element prev = null;

        while (current != null)

        {

            int curval = current.val;

// If current value is seen before

            if (hs.contains(curval)) {

                prev.next = current.next;

            } else {

                hs.add(curval);

                prev = current;

            }

            current = current.next;

        }

    }  

    /* Function to print elements in a given linked list */

    static void printList(element head)

    {

        while (head != null)

        {

            System.out.print(head.val + " ");

            head = head.next;

        }

    }

    public static void main(String[] args)

    {

        element start = new element(50);

        start.next = new element(52);

        start.next.next = new element(51);

        start.next.next.next = new element(52);

        start.next.next.next.next = new element(51);

        start.next.next.next.next.next = new element(51);

        start.next.next.next.next.next.next = new element(50);

        System.out.println("Linked list before removing duplicates :");

        printList(start);

        removeDuplicate(start);

        System.out.println("\nLinked list after removing duplicates :");

        printList(start);

    }

}


Related Solutions

Write a Java method that removes any duplicate elements from an ArrayList of integers. The method...
Write a Java method that removes any duplicate elements from an ArrayList of integers. The method has the following header(signature): public static void removeDuplicate(ArrayList<Integer> list) Write a test program (with main method) that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is what the input and output should look like:      Enter ten integers: 28 4 2 4 9 8 27 1 1 9      The distinct...
JAVA Write a method that removes the duplicate elements from an array list of integers using...
JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run: Enter ten integers: 10 20 30 20 20 30 50 60 100 9 The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
I need to implement an algorithm based on local search that are in the following list...
I need to implement an algorithm based on local search that are in the following list in Python or Matlab( simulated annealing, variable neighborhood search, variable neighborhood descent) PLEASE HELP I am really stuck!
I need to implement an algorithm based on local search that are in the following list...
I need to implement an algorithm based on local search that are in the following list in Python or Matlab( tabu search, simulated annealing, iterated local search, evolutionary local search, variable neighborhood search, variable neighborhood descent) PLEASE HELP :)
How am I supposed to implement a c_str method that returns a c string representation of...
How am I supposed to implement a c_str method that returns a c string representation of a String object? I need to return a csting representation of a string object. I am confused on what cstrings are and where to go. method signature is as follows: char* c_str();
Java Write a method that removes duplicates from an array of strings and returns a new...
Java Write a method that removes duplicates from an array of strings and returns a new array, free of any duplicate strings.
Implement function (in C programming) that calculates and returns the total size of items in a...
Implement function (in C programming) that calculates and returns the total size of items in a directory given by name. Only consider immediate contents (no need to recursively check subdirectories). Assume that appropriate header files are included (no need to specify them using #include). int dir_size(const char *name);
I need to only cout "grocery list" if the user actually entered items. If they didnt...
I need to only cout "grocery list" if the user actually entered items. If they didnt enter any items it should just cout "No need for groceries!". Everything else in the program is fine. #include <iostream> #include <vector> using namespace std; // function prototypes char chooseMenu(); vector <string> addItem(vector <string>); void showGroceries(vector <string> list); // main program int main() { vector <string> list; char choice;    cout << "Welcome to Grocery List Manager\n"; cout << "===============================\n"; do{ choice = chooseMenu();...
I need to write a method that sorts a provided Linked list with bubble sort and...
I need to write a method that sorts a provided Linked list with bubble sort and using ONLY Java List Iterator Methods (Link Below) https://docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html     public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {     // first line to start you off     ListIterator<E> iit = c.listIterator(), jit;     /**** Test code: do not modify *****/     List cc = new LinkedList(c);     Collections.sort(c);     ListIterator it1 = c.listIterator(), it2 = cc.listIterator(); while (it1.hasNext()) { if (!it1.next().equals(it2.next()))         throw new Exception("List not sorted");...
Implement a function that returns all the items in a binary search tree in order inside...
Implement a function that returns all the items in a binary search tree in order inside a std::vector. Use the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; std::vector<int> inorder_traversal(BTNode *node) { // implement } If the BST has no values, return a vector with no items in it. #include <iostream> #include <vector> class BTNode { public: int item; BTNode *left; BTNode...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT