c 语言让数组中的相同数删除,编写C程序以删除数组中的重复数字

让用户在包含重复元素的数组中输入数字。

现在,让我们编写代码以删除数组中重复的数字或元素,并创建一个包含唯一元素而不重复的数组

例如,

一个例子在下面解释-用户输入为12、30、12、45、67、30。

输出为12、30、45、67(删除重复项后)。

示例

以下是C程序删除数组中的重复数字-

#include 

#define MAX 100 // 数组的最大大小

int main(){

int array[MAX]; // 声明大小为100的数组

int size;

int i, j, k; // 循环变量

/* Input size of the array */

printf("输入数组的大小: ");

scanf("%d", &size);

/* Input elements in the array */

printf("在数组中输入元素: ");

for(i=0; i

scanf("%d", &array[i]);

}

/*find the duplicate elements in an array:

for(i=0; i

for(j=i+1; j

/* If any duplicate found */

if(array[i] == array[j]){

/* Delete the current duplicate element */

for(k=j; k

array[k] = array[k + 1];

}

/* Decrement size after removing duplicate element */

size--;

/* If shifting of elements occur then don't increment j */

j--;

}

}

}

printf("\nArray elements after deleting duplicates : ");/*print an array after deleting the duplicate elements.

for(i=0; i

printf("%d\t", array[i]);

}

return 0;

}输出结果

输出如下-

输入数组的大小: 10

在数组中输入元素: 23 12 34 56 23 12 56 78 45 56

Array elements after deleting duplicates : 23 12 34 56 78 45

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值