C语言进行数组去重代码
时间: 2023-10-18 12:19:27 浏览: 115
可以使用双重循环进行数组去重,具体代码如下:
```
#include <stdio.h>
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 5};
int len = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len;) {
if (arr[j] == arr[i]) {
for (int k = j; k < len - 1; k++) {
arr[k] = arr[k + 1];
}
len--;
} else {
j++;
}
}
}
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
这个代码可以去除数组中的重复元素,输出结果为:1 2 3 4 5。
相关问题
stl容器c语言实现数组去重
在C语言中,可以通过使用STL容器来实现数组去重的功能。STL(Standard Template Library)是C++标准库的一部分,提供了一系列的容器和算法,方便开发者进行数据结构和算法的实现。
在C语言中,可以使用哈希表来实现数组去重。哈希表是一种以键值对形式存储数据的数据结构,通过将元素的值映射到一个唯一的索引位置来实现快速的查找和插入操作。
以下是使用哈希表实现数组去重的步骤:
1. 创建一个空的哈希表。
2. 遍历原始数组中的每个元素。
3. 对于每个元素,检查哈希表中是否已经存在该元素。
- 如果存在,则说明该元素已经出现过,不需要再次插入到结果数组中。
- 如果不存在,则将该元素插入到哈希表中,并将该元素添加到结果数组中。
4. 返回结果数组,即为去重后的数组。
下面是一个示例代码:
```c
#include <stdio.h>
#include <stdbool.h>
#define HASH_SIZE 100
typedef struct Node {
int value;
struct Node* next;
} Node;
typedef struct HashTable {
Node* buckets[HASH_SIZE];
} HashTable;
void initHashTable(HashTable* hashTable) {
for (int i = 0; i < HASH_SIZE; i++) {
hashTable->buckets[i] = NULL;
}
}
int hash(int value) {
return value % HASH_SIZE;
}
bool contains(HashTable* hashTable, int value) {
int index = hash(value);
Node* node = hashTable->buckets[index];
while (node != NULL) {
if (node->value == value) {
return true;
}
node = node->next;
}
return false;
}
void insert(HashTable* hashTable, int value) {
int index = hash(value);
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->value = value;
newNode->next = hashTable->buckets[index];
hashTable->buckets[index] = newNode;
}
void removeDuplicates(int* arr, int size) {
HashTable hashTable;
initHashTable(&hashTable);
int result[size];
int resultSize = 0;
for (int i = 0; i < size; i++) {
if (!contains(&hashTable, arr[i])) {
insert(&hashTable, arr[i]);
result[resultSize++] = arr[i];
}
}
printf("去重后的数组:");
for (int i = 0; i < resultSize; i++) {
printf("%d ", result[i]);
}
printf("\n");
}
int main() {
int arr[] = {1, 2, 3, 4, 2, 3, 5};
int size = sizeof(arr) / sizeof(arr);
removeDuplicates(arr, size);
return 0;
}
```
运行以上代码,输出结果为:去重后的数组:1 2 3 4 5
用c语言格式数组去重。要求编写去重的函数。
在C语言中,由于数组的元素一旦初始化就不能动态改变大小,所以我们通常使用其他数据结构如链表或者哈希表来实现数组的去重。这里我们以使用链表为例,创建一个简单的函数来去除一维数组中的重复元素:
```c
#include <stdio.h>
#include <stdlib.h> // 需要包含malloc和free函数
// 定义链表节点结构体
typedef struct Node {
int data;
struct Node* next;
} Node;
// 创建新节点
Node* createNode(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode != NULL) {
newNode->data = value;
newNode->next = NULL;
}
return newNode;
}
// 去除数组中的重复元素
void removeDuplicates(int arr[], int n) {
Node* head = NULL;
for (int i = 0; i < n; i++) {
Node* current = head;
while (current != NULL && current->data != arr[i]) {
current = current->next;
}
if (current == NULL) {
current = createNode(arr[i]);
if (head == NULL) {
head = current;
} else {
current->next = head;
head = current;
}
}
}
}
// 打印链表
void printList(Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
int arr[] = {1, 2, 3, 2, 4, 5, 6, 4};
int n = sizeof(arr) / sizeof(arr[0]);
printf("原始数组: ");
printList(head); // 假设头指针初始为空
removeDuplicates(arr, n);
printf("去重后的数组: ");
printList(head); // 输出去重后的链表
return 0;
}
```
这段代码首先遍历数组,如果遇到的元素不在链表中,则添加到链表头部。这样就可以保证链表中的元素都是唯一的。
阅读全文
相关推荐
















