前言
这道题目网上到处都是,但是好多都没有讲清楚,然后大家又相互转载,错误泛滥,现在我来完善这道题目。
题目:每一个ip访问百度,其ip地址都会被记录到后台日志文件中,假设一天的访问日志有100G,求出一天中访问百度次数最多的ip地址,可以使用的内存大小是1G。
分析
伪代码实例
Mark for future reference hash(IP)%N get many small files int max = 0; String maxip = null; for each file Hashmap hashmap; String IP = readIP(file); if(hashmap.has(IP)) { int cnt = hashmap.get(IP); hashmap.set(IP, cnt+1); if(cnt+1 > max) { max = cnt+1; maxip = IP; } } else hashmap.put(IP, 1);