无序向量去重vector
时间: 2023-09-30 15:02:09 浏览: 53
可以使用STL中的`unique`函数来实现无序向量的去重,具体步骤如下:
1. 使用`sort`函数对无序向量进行排序,确保相同的元素相邻;
2. 使用`unique`函数将相邻的相同元素移到向量的末尾,并返回新的末尾迭代器;
3. 使用`erase`函数删除新的末尾迭代器到向量末尾的所有元素。
示例代码如下:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> nums = {1, 2, 3, 2, 4, 1, 5, 6, 5};
sort(nums.begin(), nums.end()); // 排序
auto it = unique(nums.begin(), nums.end()); // 去重
nums.erase(it, nums.end()); // 删除多余元素
for (auto num : nums) {
cout << num << " ";
}
cout << endl;
return 0;
}
```
输出结果为:`1 2 3 4 5 6`。
相关问题
无序单链表的去重c++
无序单链表的去重操作可以使用哈希表来实现。具体步骤如下:
1. 定义一个哈希表,用于记录链表中已经出现过的元素。
2. 遍历链表节点,对于每个节点,首先在哈希表中查找该节点的值是否存在。
3. 如果哈希表中已存在该值,则说明链表中已经有相同的元素,将该节点从链表中删除;如果哈希表中不存在该值,则将该值添加到哈希表中,继续遍历下一个节点。
4. 最终,遍历完整个链表后,链表中的重复元素都已经被删除。
具体实现的代码如下所示:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构
struct ListNode {
int val;
struct ListNode* next;
};
void removeDuplicates(struct ListNode* head) {
if (head == NULL) {
return;
}
// 定义哈希表
int hashSet[1000] = {0};
struct ListNode* pre = head;
struct ListNode* cur = head->next;
// 遍历链表节点
while (cur != NULL) {
// 如果哈希表中已存在该值,则删除当前节点
if (hashSet[cur->val] != 0) {
pre->next = cur->next;
free(cur);
cur = pre->next;
} else { // 否则将该值添加到哈希表中
hashSet[cur->val] = 1;
pre = cur;
cur = cur->next;
}
}
}
// 创建一个链表节点
struct ListNode* createNode(int val) {
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
node->val = val;
node->next = NULL;
return node;
}
// 打印链表
void printList(struct ListNode* head) {
struct ListNode* cur = head;
while (cur != NULL) {
printf("%d ", cur->val);
cur = cur->next;
}
printf("\n");
}
int main() {
// 创建链表示例:1->2->3->2->4->3
struct ListNode* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(2);
head->next->next->next->next = createNode(4);
head->next->next->next->next->next = createNode(3);
printf("原始链表:");
printList(head);
removeDuplicates(head);
printf("去重后的链表:");
printList(head);
return 0;
}
```
以上代码就可以实现对无序单链表进行去重操作。
用java实现对一组整数无序数组去重并排序
可以使用Java中的集合类和数组排序方法来实现对一组无序整数数组的去重并排序的操作。
```java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
public class RemoveDuplicatesAndSort {
public static void main(String[] args) {
int[] arr = {10, 5, 3, 10, 2, 5, 8, 2};
// 使用HashSet去重
HashSet<Integer> set = new HashSet<>();
for (int num : arr) {
set.add(num);
}
// 转换为List并排序
List<Integer> list = new ArrayList<>(set);
Integer[] sortedArr = list.toArray(new Integer[0]);
Arrays.sort(sortedArr);
// 输出结果
for (int num : sortedArr) {
System.out.print(num + " ");
}
}
}
```
上述代码使用HashSet去重,HashSet会自动去除重复的元素。然后将去重后的元素转换为List,并利用Arrays类的sort方法对数组进行排序。最后,遍历排序后的数组并输出结果。运行该代码,输出结果为:2 3 5 8 10。
注意,这里使用了Integer对象数组来存储去重后的元素,因为Arrays.sort方法只能对对象数组进行排序,而不能对基本类型数组进行排序。如果需要得到一个原始的int类型的数组,可以使用循环遍历将Integer对象数组转换为int类型数组。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)