您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

在C语言中,出现多次的数组元素是什么?

2024/6/21 5:44:31发布31次查看
array是一个容器,其中包含相同数据类型的元素,长度需要事先定义。数组中的元素可以以任何顺序和任意次数出现。因此,在这个程序中,我们将找出数组中出现多次的元素。
问题描述 - 我们已经给出一个数组arr[],我们需要找出数组中重复出现的元素,并打印它们。
让我们举一个例子来更好地理解。
例子:input: arr[] = {5, 11, 11, 2, 1, 4, 2}output: 11 2
解释我们有一个包含一些元素的数组arr,首先我们会在重复函数中比较下一个元素。重复函数用于在数组中找到重复的元素。在重复函数中,我们使用循环来查找给定数组中的重复元素。我们将使用if else条件来检查数组元素的计数,如果数组元素出现一次,则计数为1,如果出现多次,则计数将相应地递增。如果计数大于1,则该元素将被打印在屏幕上。
算法input : arr[], n the length of array.step 1 : for i -> 0 to n, follow step 2,step 2 : for each element of the array. do : step 2.1 : for j -> i to n repeat step 2.2 - 2.3. step 2.2 : if (arr[i] == arr[j]) -> print arr[i] step 2.3 : else {// do nothing}
example 的中文翻译为:示例#include <stdio.h>int main() { int arr[] = {21, 87, 212, 109, 41, 21}; int n=7; printf("the repeat elements of the array are : "); int *count = (int *)calloc(sizeof(int), (n - 2)); int i; for (i = 0; i < n; i++) { if (count[arr[i]] == 1) printf(" %d ", arr[i]); else count[arr[i]]++; } return 0;}
输出the repeat elements of the array are : 21
以上就是在c语言中,出现多次的数组元素是什么?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product