从大量数据中除去重复数据
来源:互联网 发布:淘宝信誉小号查询 编辑:程序博客网 时间:2024/05/15 10:30
有道题说的是,如何从大量的数据中消除重复的数据
比如有1w个数据,怎么快速的删除重复的数据呢
有一些解法是先排序,然后逐一删除
如果采取快速排序的方式呢,复杂度是O(nlogn)
接着还有遍历一边,删除重复的数据。
如果采用hash来做,似乎可以取得更好的结果:
大概方法如下:
采用取模hash函数,
找一个hash函数了,就这么映射过去,采用链接法避免冲撞
如果A 映射后的值和B,C,D...映射的相同,采用字符串匹配,如果A!=B,C,D...,把A加入链表,若相同,删除A,继续遍历
下面偶采用了map和set来实现。速度还是非常快的,测试数据10W条。
map装载(hashed number,Set)
set装载测试的数据
代码如下,在VS2005下测试通过:
如果你有更好的想法,请告诉偶
比如有1w个数据,怎么快速的删除重复的数据呢
有一些解法是先排序,然后逐一删除
如果采取快速排序的方式呢,复杂度是O(nlogn)
接着还有遍历一边,删除重复的数据。
如果采用hash来做,似乎可以取得更好的结果:
大概方法如下:
采用取模hash函数,
找一个hash函数了,就这么映射过去,采用链接法避免冲撞
如果A 映射后的值和B,C,D...映射的相同,采用字符串匹配,如果A!=B,C,D...,把A加入链表,若相同,删除A,继续遍历
下面偶采用了map和set来实现。速度还是非常快的,测试数据10W条。
map装载(hashed number,Set)
set装载测试的数据
代码如下,在VS2005下测试通过:
#include <iostream>
#include <fstream>
#include <map>
#include <set>
using namespace std;
int main()
{
ifstream inf("aa.txt");
ofstream outf("bb.txt");
if(!inf || !outf)
{
cerr<<"Can not load files"<<endl;
exit(0);
}
typedef set<int> collision_data;
map<int , collision_data *> hash_data;
const int Ihash=323;
int imod;
int itemp;
map<int , collision_data *>::iterator it;
collision_data *ctemp;
while(inf>>itemp)
{
imod=itemp%Ihash;
it=hash_data.find(imod);
//----------if we can find the hashed data,let's check the number
if(it!=hash_data.end())
{
ctemp=it->second;
//---------if can not find the number in the set,insert into the set and output to the file
if(ctemp->count(itemp)<1)
{
ctemp->insert(itemp);
outf<<itemp<<endl;//--output the number
}
}
else //we can not find the hashed data,then input it into the map
{
ctemp=new collision_data;
hash_data.insert(make_pair(imod,ctemp));
ctemp->insert(itemp);
outf<<itemp<<endl;
}
}
system("pause");
return 0;
}
#include <fstream>
#include <map>
#include <set>
using namespace std;
int main()
{
ifstream inf("aa.txt");
ofstream outf("bb.txt");
if(!inf || !outf)
{
cerr<<"Can not load files"<<endl;
exit(0);
}
typedef set<int> collision_data;
map<int , collision_data *> hash_data;
const int Ihash=323;
int imod;
int itemp;
map<int , collision_data *>::iterator it;
collision_data *ctemp;
while(inf>>itemp)
{
imod=itemp%Ihash;
it=hash_data.find(imod);
//----------if we can find the hashed data,let's check the number
if(it!=hash_data.end())
{
ctemp=it->second;
//---------if can not find the number in the set,insert into the set and output to the file
if(ctemp->count(itemp)<1)
{
ctemp->insert(itemp);
outf<<itemp<<endl;//--output the number
}
}
else //we can not find the hashed data,then input it into the map
{
ctemp=new collision_data;
hash_data.insert(make_pair(imod,ctemp));
ctemp->insert(itemp);
outf<<itemp<<endl;
}
}
system("pause");
return 0;
}
如果你有更好的想法,请告诉偶
- 从大量数据中除去重复数据
- 除去文件中的重复数据
- 除去集合中的重复数据
- mySQL大量数据中去重复查询
- Java 从MySQL中读取大量数据
- 大量的数据中除去其中有同样数的数据(树递归的一个应用)
- oracle删除大量重复数据
- 关于从大量数据中查找唯一数据的几个问题
- ORACLE删除重复记录(大量重复数据)
- 从数据库中加载大量数据的方法
- matlab读入从文件中读取大量的数据
- java正则表达式从大量日志中筛选有用数据
- 从大量数据中取最小的前k个数
- Flume OG Bug 引发大量重复数据
- Array 中除去重复值
- MATLAB数据分析与挖掘实战-数据取样-从大量的业务数据中 获取样本数据
- 利用快速排序从大量数据中查找最大的若干个数据
- 从大量的数据中找出若干个最大或者最小的数据
- 微软亚洲技术中心面试题曝光
- 欧盟咄咄逼人 微软处境不妙
- 一种软件开发的方法【原创】
- Linux下常见文件格式的压缩、解压小结
- 从800到了15000 -- 一个非科班三流大学程序员的路程。
- 从大量数据中除去重复数据
- RPM 的介绍和应用
- 如何 安装/反安装 .deb 套件包?
- ASP.NET(C#)利用QQ的IP数据库QQWry.Dat进行精确IP查询
- 这样的罚款是否合法?
- 苹果不坚持现有iPhone销售模式 将另考虑途径
- 在C++中如何将ActiveX控件所使用的BSTR数据转换成CHAR字符数组?
- 去英国工作也是个不错的选择
- 中国政府对OOXML应该投什么票?