program in java
1- Write a code to remove continuous repeatitive
elements of a Linked List.
Example:
Given: -10 -> 3 -> 3 -> 3 -> 5 -> 6
-> 6 -> 3 -> 2 -> 2 -> NULL
The answer: -10 -> 3 -> 5 -> 6 -> 3 ->
2 -> NULL
import java.util.ArrayList;
import java.util.LinkedHashSet;
public class RemoveDuplicate
{
public static void main(String[] args)
{
ArrayList<Integer> duplicateElements = new ArrayList<Integer>();
duplicateElements.add(10);
duplicateElements.add(3);
duplicateElements.add(3);
duplicateElements.add(5);
duplicateElements.add(6);
duplicateElements.add(3);
duplicateElements.add(2);
ArrayList<Integer> uniqueElements = new ArrayList<Integer>();
for(Integer ele: duplicateElements)
{
if(!uniqueElements.contains(ele))
uniqueElements.add(ele);
}
System.out.println("Duplicate element in List: " + duplicateElements);
System.out.println("Unique element in List: " + uniqueElements);
}
}
Get Answers For Free
Most questions answered within 1 hours.