# A Clear Guide on How to Remove Duplicates From a List

Are you wondering how to remove duplicates from a list? Be quick to take a look at our free sample in which this issue is explicated by an expert. After reading this sample, you will know how to deal with duplicates. On our blog, you can find many samples which can help you to solve diverse academic problems. All of them are completed by experts from AssignmentShark who possess deep knowledge in a variety of spheres.

## Remove Duplicates From an Unsorted List

We have an unsorted linked list with many random elements there. What we need to do is to find all the duplicates and remove them from the list.

Solution

There are a lot of different ways to find the duplicates in the list. We can create a lot of subarrays and put them there, or, for example, use one subarray with different keywords for each specimen of duplicates. But we will use a way that is a bit smarter. To do this, we will need a simple hash table. In the following solution, we pass through the list and add every element to the hash table. When the repeating unit is found, it is removed and the cycle continues. The whole problem can be solved in a single pass by using the linked list.

public static void removeDuplicates (Node nod) {

Hashtable hashes = new Hashtable();

node prev = null;

while (nod != null) {

if (hashes.containsKey(nod.data)) {

prev.next = nod.next;

} else {

hashes.put(nod.data, true);

prev = nod;

}

nod = nod.next;

}

}

The above solution will require O(N) time, where N is a number of elements in the linked list.

An additional constraint: the use of the buffer is forbidden. In this case, we can implement the cycle with the help of two pointers: current (running through a linked list) and moving (check any subsequent nodes for the presence of duplicates).

public static void removeDuplicates (Node head) {

while (curr != null) {

Node mov = curr;

while (mov.next != null) {

if (mov.next.data == curr.data) {

mov.next = mov.next.next;

} else{

mov = mov.next;

}

}

curr = curr.next;

}

}

This code requires only O (1) space.