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

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();
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");...
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would...
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would be the Student.h file //--------------------------------------------- #include <iostream> #include <cassert> using namespace std; // each student have a name, an ID (100000000~999999999), and three grades class Student { private: public: Student(); Student(); setName(); setId(); setGrade (); getName(); getId(); getGrade() ; printAll() ; }; //--------------------------------------------- // This would be the Student.cpp file //--------------------------------------------- //====================== YOUR CODE STARTS HERE ====================== Student::Student() //default constructor { } Student::Student(string aName,...
Design and implement a program in python that takes a list of items along with quantities...
Design and implement a program in python that takes a list of items along with quantities or weights. The program should include at least two function definition that is called within the main part of your program. Each item has a price associated by quantity or weight. The user enters the item along with the quantity or weight and the program prints out a table for each item along with the quantity/weight and total price. Your program should be able...
#2 I post this twice, please don't duplicate answer I need two different views. If you...
#2 I post this twice, please don't duplicate answer I need two different views. If you upload a photo please make sure it is clear handwriting, Thanks Discussion: Skin Color is an Illusion What to do: Please watch the TED Talk Skin Color is an Illusion Links: https://www.ted.com/talks/nina_jablonski_breaks_the_illusion_of_skin_color. and also review the site Understanding Race Links:http://understandingrace.org/home.html. from the American Anthropological Association and post what you thought was the most interesting thing that you learned.
I post this twice, please don't duplicate answer I need two different views. If you upload...
I post this twice, please don't duplicate answer I need two different views. If you upload a photo please make sure it is clear handwriting, Thanks Discussion: Skin Color is an Illusion What to do: Please watch the TED Talk Skin Color is an Illusion Links: https://www.ted.com/talks/nina_jablonski_breaks_the_illusion_of_skin_color. and also review the site Understanding Race Links:http://understandingrace.org/home.html. from the American Anthropological Association and post what you thought was the most interesting thing that you learned.
In JAVA Implement the moveMinToFront method in IntSinglyLinkedList. The moveMinToFront method looks through the list to...
In JAVA Implement the moveMinToFront method in IntSinglyLinkedList. The moveMinToFront method looks through the list to find the element with the minimum value. It moves that element to the front of the list. Before abstract view: [7, 3, 2] After Abstract view: [2,7,3] Before Abstract view: [4,1,7] After Abstract view: [1,4,7] public void moveMinToFront() { } Test: package net.datastructures; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Random; public class IntSinglyLinkedListTest { @Test public void moveMinToFrontTestEmpty() { IntSinglyLinkedList s...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT