Remove duplicate values in linked list...

HELLO FRIENDS......Welcome To Technovichar.com. This Post is About How to Remove Duplicate Values in Liked List Using Java. This Post is So Interesting and Informative for You. Check Out Full Post in Details.


Page Content :


What is Link List 
Types of Link List 
How to Remove Duplicate Value in Linked List 
How to Remove Duplicate Value in Array  
Conclusion


What is Link List ????


Link List is One Type of Data Structure. Link List Contains Nodes. Link list is Linear Data Structure. If Link List is Single then It Contain One Data Part and One Reference Part. If Link List is Doubly then It Contains One Data Part and Two Reference Part

Types of Link List 

1) Single Link List 

2) Circular Link List

3) Doubly Link List 


Here is A Program which Title is Remove Duplicate Values in Linked List. This Program is For Single Link List.


Remove_duplicate_values_in_linked_list


How to Remove Duplicate Values in Linked List????


 current = head , x = null , y=null;
  while(current!=null)
        {
            x = current ; 
            y = current.next;
            while(y!=null)
            {
                if(current.data == y.data)
                {
                    x.next = y.next;
                }
                else{
                    x = y;
                }
                y = y.next;
            }
            current = current.next;
        }



Also Read This :



Remove_duplicate_values_in_linked_list




Full Program : 


import java.util.Scanner; class SingleLinkList{
class Node{
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}

public Node head = null;
public Node tail = null; //Store Data
void adddata(int data)
{
Node newnode = new Node(data);
if(head==null)
{
head = newnode;
tail = newnode;
}
else{
tail.next = newnode;
tail = newnode;
}
}
// Remove Duplicate in Data
public void removeduplicate(){
Node current = head , x=null , y=null;
if(current==null)
{ System.out.println("List is Empty");
}
else
{
while(current!=null)
{ x = current ;
y = current.next;
while(y!=null)
{ if(current.data == y.data)
{
x.next = y.next;
}
else{
x = y;
}
y = y.next;
}
current = current.next;
}
}
}

//Display Link List
public void display(){
Node mainnode = head;
if(mainnode ==null)

{ System.out.println("List is Empty");
}
else{
while(mainnode!=null)
{
System.out.println(mainnode.data);
mainnode = mainnode.next;
}

}
}
}

public class Main
{
public static void main(String[] args) {
SingleLinkList s1 = new SingleLinkList(); Scanner s = new Scanner(System.in);
while(true)
{
System.out.println("1.Add Data \n 2.Display Data \n 3.Remove Duplicate Data \n 4.Exit");
int ch =s.nextInt();
switch(ch)
{
case 1:System.out.println("Enter Data To Insert");
int a = s.nextInt();
s1.adddata(a);
break;
case 2:s1.display();
break;
case 3:s1.removeduplicate();
System.out.println("Duplicate Element Removed Successfully");
break;
case 4:return;
default:System.out.println("Please Enter Valid Value");
} } } }



How to Remove Duplicate Values in Array using Java????



//How to Remove Duplicate Values in Java
import java.util.Arrays;
public class Main
{
public static void main(String[] args) {
int arr[] =new int[]{1,2,2,3,4,4,1,2,4,5,67,89};
// First Sort the Array
Arrays.sort(arr);
int length = getremovedulplicate(arr,arr.length);
for(int i=0;i<length;i++)
{
System.out.println(arr[i]);
} }
static int getremovedulplicate(int arr[],int x)
{
int h1[] = new int[x];
int j=0;
if(x==0 || x==1)
{
return 1;
}
for(int i=0;i<x-1;i++)
{
if(arr[i]!=arr[i+1])
{
h1[j++] = arr[i];
}
}

h1[j++] = arr[x-1];
for(int i=0;i<j;i++)
{
arr[i] = h1[i];
} return j; } }


Conclusion :


This is All About how to Remove Duplicate Values in Link list and Array. If You Like This Article Content Then Like and Share it With Your Friends and Family.


About Author: 

Hello friends, I am Abhi Patel author & founder of Technovichar.com. I am a student of IT engineering. I like to research and write about new things even if it is from any field. You will continue to support us in this way, we will keep on bringing such interesting information for you.

Post a Comment

0 Comments