<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p>

<p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p>

<p>You must solve the problem <strong>without</strong> modifying the array <code>nums</code>&nbsp;and uses only constant extra space.</p>

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

<pre>
<strong>Input:</strong> nums = [1,3,4,2,2]
<strong>Output:</strong> 2
</pre>

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

<pre>
<strong>Input:</strong> nums = [3,1,3,4,2]
<strong>Output:</strong> 3
</pre>

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

<pre>
<strong>Input:</strong> nums = [3,3,3,3,3]
<strong>Output:</strong> 3</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>nums.length == n + 1</code></li>
	<li><code>1 &lt;= nums[i] &lt;= n</code></li>
	<li>All the integers in <code>nums</code> appear only <strong>once</strong> except for <strong>precisely one integer</strong> which appears <strong>two or more</strong> times.</li>
</ul>

<p>&nbsp;</p>
<p><b>Follow up:</b></p>

<ul>
	<li>How can we prove that at least one duplicate number must exist in <code>nums</code>?</li>
	<li>Can you solve the problem in linear runtime complexity?</li>
</ul>

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

class Solution {
public:
    // Linked List Node
    struct node {
        // key and value are both integers
        int key;
        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;
        }
    }

    // Hash function for int keys
    int hashFunction(struct hashMap* mp, int key) {
        // Simple hash function based on modulus
        return (abs(key) / 10) % mp->capacity; // Use absolute value to avoid negative indices
    }

    // 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
    int 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 bucketHead->value;
            }
            bucketHead = bucketHead->next;
        }

        return -1; // Key not found
    }

    // Function to detect if there is any duplicate
    int findDuplicate(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]) != -1) {
                // Duplicate exists
                freeHashMap(mp);  // Ensure to free allocated memory
                return nums[i];
            } else {
                // Element doesn't exist in the HashMap, add it
                insert(mp, nums[i], nums[i]);
            }
        }

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

    // 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);
    }
};

74786