写一个 c语言 去重函数
时间: 2024-01-20 19:04:14 浏览: 41
以下是一个简单的C语言去重函数:
```c
void removeDuplicates(int arr[], int n) {
int i, j, k;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n;) {
if (arr[j] == arr[i]) {
for (k = j; k < n; k++) {
arr[k] = arr[k + 1];
}
n--;
} else {
j++;
}
}
}
}
```
这个函数接受一个int类型数组和它的大小作为参数。它使用两个嵌套的循环来迭代数组,找到重复的元素并将它们删除。最后,函数返回一个不带重复元素的数组。
相关问题
c语言数组去重函数调用
在C语言中,可以通过编写一个数组去重函数来实现对数组中重复元素的去除。以下是一个示例的数组去重函数调用的介绍:
假设我们有一个整型数组`arr`,长度为`n`,我们想要对该数组进行去重操作。可以按照以下步骤进行:
1. 定义一个新的数组`result`,用于存储去重后的元素。
2. 遍历原始数组`arr`,逐个检查每个元素是否已经存在于`result`数组中。
3. 如果当前元素不存在于`result`数组中,则将其添加到`result`数组中。
4. 最后,`result`数组中存储的就是去重后的元素。
下面是一个示例的C语言代码,展示了如何调用一个数组去重函数:
```c
#include <stdio.h>
// 数组去重函数
int* removeDuplicates(int* arr, int n, int* resultSize) {
int* result = (int*)malloc(n * sizeof(int)); // 分配内存空间
int count = 0; // 记录去重后的元素个数
for (int i = 0; i < n; i++) {
int j;
for (j = 0; j < count; j++) {
if (arr[i] == result[j]) {
break; // 当前元素已存在于result数组中,跳出内层循环
}
}
if (j == count) {
result[count++] = arr[i]; // 当前元素不存在于result数组中,添加到result数组中
}
}
*resultSize = count; // 更新去重后的元素个数
return result;
}
int main() {
int arr[] = {1, 2, 3, 2, 4, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int resultSize;
int* result = removeDuplicates(arr, n, &resultSize);
printf("去重后的数组:");
for (int i = 0; i < resultSize; i++) {
printf("%d ", result[i]);
}
free(result); // 释放内存空间
return 0;
}
```
运行上述代码,输出结果为:
```
去重后的数组:1 2 3 4 5
```
写一个冗余数据去重算法的代码? 用 C语言写
### 回答1:
#include <stdio.h>
int main(){
int arr[10], i, j, k, size;
printf("\nEnter size of array: ");
scanf("%d", &size);
printf("\nEnter %d elements in array: ", size);
for(i=0; i<size; i++){
scanf("%d", &arr[i]);
}
printf("\nThe array elements after removing redundancies are: ");
for(i=0; i<size; i++){
for(j=i+1; j<size; j++){
if(arr[i]==arr[j]){
for(k=j; k<size; k++){
arr[k]=arr[k+1];
}
size--;
j--;
}
}
}
for(i=0; i<size; i++){
printf("%d ", arr[i]);
}
return 0;
}
### 回答2:
以下是使用C语言编写的一个简单的冗余数据去重算法:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点
typedef struct Node {
int value;
struct Node* next;
} Node;
// 链表的插入操作
void insert(Node** head, int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->value = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// 链表的删除操作
void removeDuplicates(Node** head) {
if (*head == NULL) {
return;
}
Node* current = *head;
while (current != NULL) {
Node* runner = current;
while (runner->next != NULL) {
if (runner->next->value == current->value) {
Node* duplicate = runner->next;
runner->next = runner->next->next;
free(duplicate);
} else {
runner = runner->next;
}
}
current = current->next;
}
}
// 遍历链表并打印结果
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->value);
temp = temp->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
// 添加示例数据
insert(&head, 5);
insert(&head, 2);
insert(&head, 3);
insert(&head, 2);
insert(&head, 1);
insert(&head, 5);
insert(&head, 4);
printf("原始数据:");
printList(head);
printf("去重后的数据:");
removeDuplicates(&head);
printList(head);
return 0;
}
```
该算法使用链表数据结构来存储数据,并使用两个嵌套的循环遍历链表,找到重复的数据节点并删除。最后,打印去重后的链表结果。
注意:上述代码只是一个简单的冗余数据去重示例,实际情况下根据具体需求可能需要更复杂的算法和数据结构。
### 回答3:
冗余数据去重算法可以通过使用哈希表来实现。以下是用 C 语言编写的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 10000 // 哈希表的大小
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* hashTable[TABLE_SIZE]; // 哈希表
// 初始化哈希表
void initHashTable() {
for (int i = 0; i < TABLE_SIZE; ++i) {
hashTable[i] = NULL;
}
}
// 哈希函数
int hash(int data) {
return abs(data) % TABLE_SIZE;
}
// 查找元素
Node* search(int data) {
int index = hash(data);
Node* p = hashTable[index];
while (p != NULL) {
if (p->data == data) {
return p;
}
p = p->next;
}
return NULL;
}
// 插入元素
void insert(int data) {
int index = hash(data);
if (search(data) != NULL) {
return;
}
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = hashTable[index];
hashTable[index] = newNode;
}
// 冗余数据去重
void removeRedundancy(int* arr, int n) {
initHashTable();
for (int i = 0; i < n; ++i) {
insert(arr[i]);
}
for (int i = 0; i < TABLE_SIZE; ++i) {
Node* p = hashTable[i];
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 1, 2, 3};
int n = sizeof(arr) / sizeof(arr[0]);
removeRedundancy(arr, n);
return 0;
}
```
以上代码通过哈希表来存储数据,并查找和插入数据时根据哈希值来定位。通过遍历数组中的元素,将不重复的元素插入到哈希表中。最后,遍历哈希表并输出去重后的数据。运行以上代码,输出结果为`1 2 3 4 5`,去重成功。需要注意的是,由于哈希表的大小是有限的,可能会出现哈希冲突,但本例中的哈希函数采用取绝对值再取模的方式,相对较为简单,可以满足基本的去重需求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)