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 :)
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();...
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...
**Program #2: Working with Generics Implement the following method that returns the maximum element in an...
**Program #2: Working with Generics Implement the following method that returns the maximum element in an array. public static <E extends Comparable<E>> E max(E[] list) What should you do?      Write a program to test the method above with various types. Deliverables: There are 2 separate programs to complete. Place them both in the same package. Projects submitted with evidence of plagiarism will be given a score of 0. Java files (source code) Word document should include: Screen snapshots of...
#2 I post this twice, please don't duplicate answer I need two different views. Thanks Discussion:...
#2 I post this twice, please don't duplicate answer I need two different views. Thanks Discussion: Inner Neandertal What to do: Please watch the TED talk DNA Clues into Our inner Neandertal Links: https://www.ted.com/talks/svante_paeaebo_dna_clues_to_our_inner_neanderthal. and then read a recent article, Neanderthals and Moderns Humans Interbred '10,000 years ago, Links: http://www.bbc.com/news/science-environment-35595661. and post about something that you learned from a both and whether or not you thought the two contradicted one another. PLEASE COPY LINKS AND PAST
#1 I post this twice, please don't duplicate answer I need two different views. Thanks Discussion:...
#1 I post this twice, please don't duplicate answer I need two different views. Thanks Discussion: Inner Neandertal What to do: Please watch the TED talk DNA Clues into Our inner Neandertal Links: https://www.ted.com/talks/svante_paeaebo_dna_clues_to_our_inner_neanderthal. and then read a recent article, Neanderthals and Moderns Humans Interbred '10,000 years ago, Links: http://www.bbc.com/news/science-environment-35595661. and post about something that you learned from a both and whether or not you thought the two contradicted one another.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT