<p>Given an integer array <code>nums</code>, return <code>true</code> if any value appears <strong>at least twice</strong> in the array, and return <code>false</code> if every element is distinct.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,1]</span></p>

<p><strong>Output:</strong> <span class="example-io">true</span></p>

<p><strong>Explanation:</strong></p>

<p>The element 1 occurs at the indices 0 and 3.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4]</span></p>

<p><strong>Output:</strong> <span class="example-io">false</span></p>

<p><strong>Explanation:</strong></p>

<p>All elements are distinct.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,3,3,4,3,2,4,2]</span></p>

<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    class MyHashSingle{
    private:
        vector<int> table;
        int hash(int value){
            long newValue = value;
            long index = (newValue * newValue) % table.size();
            return index;
        }
        bool esPrimo(int n){
            if (n==2) return true;
            if (n%2==0) return false;
            int lim = sqrt(n);
            for (int k=3; k<=lim; k++)
                if (n%k==0) return false;
            return true;
        }
    public:
        MyHashSingle(vector<int>& nums){
            int prime = (nums.size()*2) + 1;        
            while (!esPrimo(prime)) prime = prime + 1;
            table.resize(prime, -1);
            int len = nums.size();
            for (int k=0; k<len; k++){
                int pos = hash(nums[k]);
                bool repetido = false;
                while (table[pos] != -1) {
                    if (table[pos] == nums[k]) repetido = true;
                    pos = (pos+1) % prime;
                }
                if (!repetido) table[pos] = nums[k];
            }
        }

        MyHashSingle(vector<int>& nums, bool empty){
            int prime = (nums.size()*2) + 1;        
            while (!esPrimo(prime)) prime = prime + 1;

            table.resize(prime, INT_MIN);
        }

        void insert(int value){
            int pos = hash(value);
            bool repetido = false;
            while (table[pos] != INT_MIN) {
                if (table[pos] == value) repetido = true;
                pos = (pos+1) % table.size();
            }
            if (!repetido) table[pos] = value;
        }

        void showTable(){
            int len = table.size();
            for (int k=0; k<len; k++) cout << table[k] << " ";
            cout << endl;
        }

        int find(int n){
            int pos = hash(n);
            while (table[pos] != INT_MIN){
                if (table[pos] == n) return true;
                pos = (pos + 1) % table.size();
            }
            return false;
        }

        vector<int> rebuild(){
            vector<int> tmp;
            int len = table.size();
            for (int k=0; k<len; k++) 
                if (table[k]!=-1) tmp.push_back(table[k]);
            return tmp;
        }
    };

    bool containsDuplicate(vector<int>& nums) {
        MyHashSingle hash1(nums, true);
        for (int value:nums){
            if (hash1.find(value)) return true;
            else hash1.insert(value);
        }
        return false;
    }
};
64034