头文件 #include<unordered_set> ,基于哈希表

    1. unordered_set<int> set;
    2. for (int i = 0; i < list.size(); i++) {
    3. set.insert(list[i]);
    4. }
    5. for (unordered_set<int>::iterator i = set.begin(); i != set.end(); i++) {
    6. cout << *i << endl;
    7. }
    8. cout << "Find 39: " << *set.find(39) << endl;
    9. cout << "Count 5:" << set.count(5) << endl;

    我在 面试题 17.04. 消失的数字 中使用👇

    1. class Solution {
    2. public:
    3. int missingNumber(vector<int>& nums) {
    4. int length=nums.size();
    5. unordered_set<int> hashset;
    6. for(int i=0;i<=length;i++){hashset.insert(i);}
    7. for(int i=0;i<length;i++){
    8. auto iter=hashset.find(nums[i]);
    9. hashset.erase(iter);
    10. }
    11. return *hashset.begin();
    12. }
    13. };