/* Using Array */
#include<stdio.h>
int main() {
int arr[10], i, j, k, size;
printf("\nEnter array size : ");
scanf("%d", &size);
printf("\nAccept Numbers : ");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("\nArray with Unique list : ");
for (i = 0; i < size; i++)
{
for (j = i + 1; j < size;)
{
if (arr[j] == arr[i])
{
for (k = j; k < size; k++)
{
arr[k] = arr[k + 1];
}
size--;
}
else
j++;
}
}
/*Printing the Result */
for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
return (0);
}
/* Using Linked List */
#include<stdio.h>
#include<stdlib.h>
/* A linked list node */
typedef struct node
{
int data;
struct node *next;
} node;
/* Function to remove duplicates from a unsorted linked list */
void rmvDup(node *start)
{
node *ptr1, *ptr2, *dup;
ptr1 = start;
/* Pick elements one by one */
while(ptr1 != NULL && ptr1->next != NULL)
{
ptr2 = ptr1;
/* Compare the picked element with rest of the elements */
while(ptr2->next != NULL)
{
/* If duplicate then delete it */
if(ptr1->data == ptr2->next->data)
{
dup = ptr2->next;
ptr2->next = ptr2->next->next;
free(dup);
}
else
{
ptr2 = ptr2->next;
}
}
ptr1 = ptr1->next;
}
}
/* Function to push a node */
void push(node** head_ref, int new_data);
/* Function to print nodes in a given linked list */
void printList(node *node);
int main()
{
node *start = NULL;
/* The constructed linked list is:
10,20,30,20,30,40,50,40 */
push(&start, 10);
push(&start, 20);
push(&start, 30);
push(&start, 20);
push(&start, 30);
push(&start, 40);
push(&start, 50);
push(&start, 40);
printf("\n List of data : ");
printList(start);
rmvDup(start);
printf("\n\n List after removing duplicates : ");
printList(start);
getchar();
}
/* Function to push a node */
void push(node** head_ref, int new_data)
{
/* allocate node */
node* new_node = (node*) malloc(sizeof(node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print nodes in a given linked list */
void printList(node *node)
{
while(node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
0 Comments