点击复制后,将打开C知道体验页 | 帮我写一个c语言代码,将一个数组内重复的数字只输出一次
时间: 2024-05-12 08:17:26 浏览: 68
Certainly! Here's the C code to remove duplicate elements from an array:
```c
#include <stdio.h>
int main()
{
int arr[] = {2, 4, 2, 1, 5, 6, 8, 5, 9};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < i; j++)
{
if (arr[i] == arr[j])
{
break;
}
}
if (i == j)
{
printf("%d ", arr[i]);
}
}
return 0;
}
```
In this code, we first initialize an array `arr` with some duplicate elements. We then calculate the length `n` of the array. Next, we use two nested loops to check for duplicate elements. The outer loop iterates through all the elements of the array, while the inner loop compares each element with all the elements before it. If a duplicate element is found, the inner loop is broken, and we move on to the next element. If a unique element is found, we print it out.
The output of this program should be:
```
2 4 1 5 6 8 9
```
This code will remove all duplicates and print only the unique elements of the array. Let me know if you have any questions!
阅读全文