<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 <sys/mman.h>

struct Solution {
    static bool containsDuplicate(const vector<int>& nums) {
        static_assert(CHAR_BIT == 8);
        static_assert(sizeof(int) == 4);
        static_assert(sizeof(uintptr_t) == 8);

        constexpr auto len = size_t{1} << (32 - 3);
        void* const mapped = mmap(nullptr, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        if (mapped == MAP_FAILED) {
            throw std::system_error(errno, std::generic_category(), "mmap");
        }
        if (madvise(mapped, len, MADV_HUGEPAGE) == -1) {
            throw std::system_error(errno, std::generic_category(), "madvise");
        }

        const auto bitmap = reinterpret_cast<uintptr_t*>(mapped);
        uintptr_t any = 0;

        if ((reinterpret_cast<uintptr_t>(nums.data()) % 8) != 0) {
            throw std::runtime_error("Misaligned data");
        }

        #pragma GCC unroll 8
        #pragma GCC ivdep
        for (ptrdiff_t i = 0; i < nums.size(); ++i) {
            const auto u = static_cast<unsigned>(nums[i]);
            uintptr_t& bits = bitmap[u >> 6];
            uintptr_t mask = uintptr_t{1} << (u & 0x3f);
            any |= bits & mask;
            bits |= mask;
        }

        return !!any;
    }
};
62590