Q24. Write a code to delete duplicate elements from array
Delete Duplicate Elements :- In this, we will perform the delete operation in the array and after performing the delete operation, the array must contain only unique integer values.
For Example :-
Input = 5,7,8,2,4,5,6,5,4,4
Output = 5,7,8,2,4,6
So as we can see that the input consists of 10 elements ( 5,7,8,2,4,5,6,5,4,4) which contain some duplicate elements. So we have to remove duplicate elements from the array, and after performing the delete operation the output will be 5,7,8,2,4,6
Algorithm For Delete Duplicate Elements
START
Step 1 -> Input the number of elements of the array.
Step 2 -> Input the array elements.
Step 3 -> Repeat from i = 1 to n
– if (arr[i] != arr[i+1])
– temp[j++] = arr[i]
– temp[j++] = arr[n-1]
Step 4 -> Repeat from i = 1 to j
– arr[i] = temp[i]
Step 5 -> return j.
STOP
Code For Delete Duplicate Elements
#include <stdio.h>
int main()
{
int arr[10]={#include <stdio.h>
int main()
{
int arr[10]={5,7,8,2,4,5,6,5,4,4};
int size=10;
int i, j, k; // Loop control variables
/*
* Find duplicate elements in array
*/
for(i=0; i< size; i++)
{
for(j=i+1; j< size; j++)
{
/* If any duplicate found */
if(arr[i] == arr[j])
{
/* Delete the current duplicate element */
for(k=j; k< size; k++)
{
arr[k] = arr[k + 1];
}
/* Decrement size after removing duplicate element */
size–;
/* If shifting of elements occur then don’t increment j */
j–;
}
}
}
/*
* Print array after deleting duplicate elements
*/
printf(“Array elements after deleting duplicates : “);
for(i=0; i< size; i++)
{
printf(“%d “, arr[i]);
}
return 0;
}};
int size=10;
int i, j, k; // Loop control variables
/*
* Find duplicate elements in array
*/
for(i=0; i< size; i++)
{
for(j=i+1; j< size; j++)
{
/* If any duplicate found */
if(arr[i] == arr[j])
{
/* Delete the current duplicate element */
for(k=j; k< size; k++)
{
arr[k] = arr[k + 1];
}
/* Decrement size after removing duplicate element */
size–;
/* If shifting of elements occur then don’t increment j */
j–;
}
}
}
/*
* Print array after deleting duplicate elements
*/
printf(“Array elements after deleting duplicates : “);
for(i=0; i< size; i++)
{
printf(“%d “, arr[i]);
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
int i,j,k,size=10,arr[30]={5,7,8,2,4,5,6,5,4,4};
for(i=0;i< size;++i)
for(j=i+1;j< size;)
{
if(arr[i]==arr[j])
{
for(k=j;k< size-1;++k)
arr[k]=arr[k+1];
–size;
}
else
++j;
}
cout<<“Array elements after deleting duplicates : “;
for(i=0;i< size;++i)
cout<< arr[i]<<” “;
return 0;
}
import java.util.Arrays;
import java.util.LinkedHashSet;
public class Main
{
public static void main(String[] args) throws CloneNotSupportedException
{
//Array with duplicate elements
Integer[] numbers = new Integer[] {5,7,8,2,4,5,6,5,4,4};
//Create set from array elements
LinkedHashSet< Integer > linkedHashSet = new LinkedHashSet< >( Arrays.asList(numbers) );
//Get back the array without duplicates
Integer[] numbersWithoutDuplicates = linkedHashSet.toArray(new Integer[] {});
System.out.print(“Array elements after deleting duplicates : “);
//Verify the array content
System.out.println( Arrays.toString(numbersWithoutDuplicates) );
}
}
mylist = [5,7,8,2,4,5,6,5,4,4]
mylist = list(dict.fromkeys(mylist))
print(“Array elements after deleting duplicates : “)
print(mylist)
$arr = array(5,7,8,2,4,5,6,5,4,4);
$result = array_unique($arr);
print(“Array elements after deleting duplicates : “);
foreach ($result as $value) {
print_r($value);
echo ” “;
}
Output
Array elements after deleting duplicates :5,7,8,2,4,6,
Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number