<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>

#include <iostream>
#include <vector>
#include <cstdlib> // For malloc and free

class Solution {
public:
    // Linked List Node
    struct node {
        int key;  // key and value are both integers
        int value;
        struct node* next;
    };

    // Function to initialize a node
    void setNode(struct node* n, int key, int value) {
        n->key = key;
        n->value = value;
        n->next = nullptr;
    }

    // HashMap structure
    struct hashMap {
        int numOfElements, capacity;
        struct node** arr;
    };

    // Function to initialize the hashMap
    void initializeHashMap(struct hashMap* mp) {
        mp->capacity = 100;  // Set capacity to 100
        mp->numOfElements = 0;
        mp->arr = (struct node**)malloc(sizeof(struct node*) * mp->capacity);
        // Initialize all buckets to NULL to avoid undefined behavior
        for (int i = 0; i < mp->capacity; ++i) {
            mp->arr[i] = nullptr;
        }
    }

    // Improved Hash function for int keys
    int hashFunction(struct hashMap* mp, int key) {
        // Use modulus to keep index within bounds and handle negative keys
        return (key % mp->capacity + mp->capacity) % mp->capacity;
    }

    // Function to insert a key-value pair into the hashMap
    void insert(struct hashMap* mp, int key, int value) {
        int bucketIndex = hashFunction(mp, key);
        struct node* newNode = (struct node*)malloc(sizeof(struct node));
        setNode(newNode, key, value);

        if (mp->arr[bucketIndex] == nullptr) {
            mp->arr[bucketIndex] = newNode;
        } else {
            newNode->next = mp->arr[bucketIndex];
            mp->arr[bucketIndex] = newNode;
        }
        mp->numOfElements++;
    }

    // Function to search for a key in the hashMap
    bool search(struct hashMap* mp, int key) {
        int bucketIndex = hashFunction(mp, key);
        struct node* bucketHead = mp->arr[bucketIndex];

        while (bucketHead != nullptr) {
            if (bucketHead->key == key) {
                return true;  // Key found
            }
            bucketHead = bucketHead->next;
        }

        return false;  // Key not found
    }

    // Function to detect if there is any duplicate
    bool containsDuplicate(std::vector<int>& nums) {
        // Initialize hash map
        struct hashMap* mp = (struct hashMap*)malloc(sizeof(struct hashMap));
        initializeHashMap(mp);

        // Search for duplicates using the Hash Map
        for (int i = 0; i < nums.size(); i++) {
            if (search(mp, nums[i])) {
                // Duplicate exists
                freeHashMap(mp);  // Ensure to free allocated memory
                return true;
            } else {
                // Element doesn't exist in the HashMap, add it
                insert(mp, nums[i], nums[i]);
            }
        }

        freeHashMap(mp);  // Ensure to free allocated memory
        return false;
    }

    // Function to free the allocated memory of the hashMap
    void freeHashMap(struct hashMap* mp) {
        for (int i = 0; i < mp->capacity; ++i) {
            struct node* current = mp->arr[i];
            while (current != nullptr) {
                struct node* temp = current;
                current = current->next;
                free(temp);
            }
        }
        free(mp->arr);
        free(mp);
    }
};
66303