C / C ++程序从数组中删除重复的元素

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

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值