您的位置:首页 > 编程语言 > C语言/C++

删除数组中重复元素--哈希表方法和set方法

2016-09-07 11:46 661 查看
忘了是哪个的面试题,说删除数组中重复元素,给了一组数, [90, 91, 1, 2, 90, 5, 91], 输出 [90, 91, 1, 2, 5],类似这种情况,我给出了两种解决方案。

其实最简单的应该是申请一个等大的数组,去循环遍历,我没有采用。我采用以下两种方法。

一:相似因子法(我自创的)

按照每个数的个位划分,个位一共有0~9十种情况,我将个位相同的放在同一个链地址中,只需申请一个每个成员为结构体,共有10个成员的数组。这在数据较大时查找前面相同元素能提高很大效率的。

代码如下:

#include <iostream>
#include <assert.h>
using namespace std;

const int N = 10;

typedef struct similar_factor{
int val;
struct similar_factor* next;
similar_factor(int v) : val(v), next(NULL) {}
similar_factor() {}
} similar_factor;

int fill_in(similar_factor* factor, const int value, int index)
{
similar_factor* new_factor = new similar_factor(value);
if(factor[index].next == NULL){
new_factor->next = factor[index].next;
factor[index].next = new_factor;
}
else{
similar_factor *tmp = &factor[index];

while(tmp->next != NULL){
if(tmp->next->val == new_factor->val)
return -1;
tmp = tmp->next;
}
tmp->next = new_factor;
}
return 0;
}

int main()
{
int array[] = {90, 91, 1, 2, 90, 5, 91};

similar_factor* factor = new similar_factor[10];
assert(factor != NULL);

for(int i=0; i<sizeof(array)/sizeof(int); ++i){
int num = array[i] % 10;
if(fill_in(factor, array[i], num) == 0)
cout << array[i] << ' ';
else
continue;
}
delete []factor;

return 0;
}

二:set集合方法

我们知道C++的set底层是一颗红黑树,之前有写过红黑树的代码,知道它的机制是插入元素如果碰见相同的元素,直接插入失败,返回false,所以我们可以利用这个机制来做这道题。

代码如下:

#include <iostream>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
int array[] = {90, 91, 1, 2, 90, 5, 91};

set<int> sint;
size_t len = sizeof(array) / sizeof(int);
for(int i=0; i<len; ++i){
if(sint.insert(array[i]).second)
cout << array[i] << ' ';
}

return 0;
}

以上代码均经过测试,结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息