find the two same numbers in 1 million random numbers
2013-03-27 17:33
337 查看
Problem:
There are two same numbers and other unique numbers in a set which contains 1 million random numbers totally. Find out the two same numbers.
Ideas:
If the range of numbers is small, we can use two bitmaps to solve it simply. If the range is too large, it seems to be a reasonable method using a hash function to map numbers into the range [0, 1m] . We notice that numbers in this set is random, so we
can easily select "module/1m" as hash function.
Let's compute the probability of collision of our hash function for our random numbers. Actually, it's not small! The probability is about 0.36 if random integers are generated from [1, MAX_INT]. It's easy to identify
we can't decrease the probability of collision by selecting proper hash function. So can save about 3/5 memory by using this method than using tree-map directly.
Above methods is predicated on the random numbers are generated from an range uniformly.
Solution:
There are two same numbers and other unique numbers in a set which contains 1 million random numbers totally. Find out the two same numbers.
Ideas:
If the range of numbers is small, we can use two bitmaps to solve it simply. If the range is too large, it seems to be a reasonable method using a hash function to map numbers into the range [0, 1m] . We notice that numbers in this set is random, so we
can easily select "module/1m" as hash function.
Let's compute the probability of collision of our hash function for our random numbers. Actually, it's not small! The probability is about 0.36 if random integers are generated from [1, MAX_INT]. It's easy to identify
we can't decrease the probability of collision by selecting proper hash function. So can save about 3/5 memory by using this method than using tree-map directly.
Above methods is predicated on the random numbers are generated from an range uniformly.
Solution:
template <size_t size> int find(int v[]) { std::bitset<size> indicator; std::map<int, int> collision; int pos; for(int i = 0; i < size; i++) { pos = hash(v[i]); if (indicator.test(pos)) { collision.insert(std::make_pair(v[i], 0)); } else { indicator.set(pos); } } std::cerr<<"map size:"<<collision.size()<<std::endl; std::map<int, int>::iterator iter; for(int i = 0; i < size; i++) { iter = collision.find(v[i]); if (iter != collision.end()) { iter->second += 1; if (iter->second == 2) { return v[i]; } } } return -1; }
相关文章推荐
- Data Structure Array: Find the two numbers with odd occurrences in an unsorted array
- Ch2-5: find the beginning of loop in a circular linked list---two solutions
- two or more web modules defined in the configuration have the same context root
- Two or more Web modules defined in the configuration have the same context root (/ecsr).
- Find the two non-repeating elements in an array of repeating elements
- It is not possible to run two different versions of ASP.NET in the same IIS process
- idea 错误 Two modules in a project cannot share the same content root
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- Compare Data from the Same Table in two Different Environments
- Find the nearest common ancestor of any two nodes in a binary search tree
- Find the kth smallest element in two sorted array
- Two or more Web modules defined in the configuration have the same context root (/bms).
- 数组空间Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- two or more web modules defined in the configuration have the same context root
- You are to find all the two-word compound words in a dictionary. A two-word compound word is a word
- Find the common ancestor of two nodes in a binary tree
- How to make two <div>…</div> in the same row?
- Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- Problem 10 of Find the sum of all the primes below two million.
- Find The Shortest Path In Triangle Numbers