Here you will get C and C++ program to remove duplicate elements from array.
在这里,您将获得C和C ++程序,以从数组中删除重复的元素。
Given Array: 5 8 9 5 12 9
给定数组:5 8 9 5 12 9
New Array: 5 8 9 12
新阵列:5 8 9 12
In this program I have compared each element with all other elements. If the element is equal to any other element in the array then shift all the elements to left by one place. The process is repeated till all the elements are compared.
在此程序中,我将每个元素与所有其他元素进行了比较。 如果该元素等于数组中的任何其他元素,则将所有元素向左移动一位。 重复该过程,直到比较所有元素。
C / C ++程序从数组中删除重复的元素 (C/C++ Program to Remove Duplicate Elements From Array)
C程序 (C Program)
#include<stdio.h>
int main()
{
int i,j,k,n,a[30];
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter elements of array\n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
for(i=0;i<n;++i)
for(j=i+1;j<n;)
{
if(a[i]==a[j])
{
for(k=j;k<n-1;++k)
a[k]=a[k+1];
--n;
}
else
++j;
}
printf("\n");
for(i=0;i<n;++i)
printf("%d ",a[i]);
return 0;
}
C ++程序 (C++ Program)
#include<iostream>
using namespace std;
int main()
{
int i,j,k,n,a[30];
cout<<"How many elements?";
cin>>n;
cout<<"\nEnter elements of array\n";
for(i=0;i<n;++i)
cin>>a[i];
for(i=0;i<n;++i)
for(j=i+1;j<n;)
{
if(a[i]==a[j])
{
for(k=j;k<n-1;++k)
a[k]=a[k+1];
--n;
}
else
++j;
}
cout<<"\n";
for(i=0;i<n;++i)
cout<<a[i]<<" ";
return 0;
}
Output
输出量
How many elements?5
有多少元素?5
Enter elements of array 12 4 6 4 2
输入数组 12 4 6 4 2的 元素
12 4 6 2
12 4 6 2
翻译自: https://www.thecrazyprogrammer.com/2015/06/cc-program-to-remove-duplicate-elements.html