✅Beats 96% |🔥 Easy, Fast and Efficient using HashSet🔥🔥O(n) time and space complexity 🔥2 pass🔥
\n\n\n\n# Intuition\nTo find the largest positive integer k such that -k also exists in the array, we can make array into hashset so that search takes O(1) time
![image.png](https://assets.leetcode.com/users/images/cd3d80f3-90f6-412f-b227-6a4b013d3e6e_1714609588.3805377.png)\n\n\n\n# Intuition\nTo find the largest positive integer k such that -k also exists in the array, we can make array into hashset so that search takes O(1) time complexity. And iterate through the hashset and check if the negative complement of each number exists in the hashset. Storing max number satisfying given condition.\n\n# Approach\nWe can use a set to efficiently check for the existence of numbers in the array. We initialize the result variable `res` to -1 incase no number is found satisfying giving condition and iterate through the set of numbers in the array. For each number `num`, we check if its negative counterpart `-num` exists in the array. If it does, we update the result `res` to be the maximum of the current `res` and `num`.\n\n# Complexity\n- Time complexity: $$O(n)$$, where n is the number of elements in the array. The loop iterates through the array once.\n- Space complexity: $$O(n)$$,  due to the set `nums`, which stores the distinct elements of the array.\n\n\n# Code\n``` python []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        res = -1\n        nums = set(nums)\n        for num in nums:\n            if num * -1 in nums:\n                res = max(res, num)\n        return res\n```\n```java []\nclass Solution {\n    public int findMaxK(int[] nums) {\n      int res = -1;\n        Set<Integer> numSet = new HashSet<>();\n        for (int num : nums) {\n            numSet.add(num);\n        }\n        for (int num : numSet) {\n            if (numSet.contains(-num)) {\n                res = Math.max(res, num);\n            }\n        }\n        return res;\n    }\n};\n```\n```cpp []\nclass Solution {\npublic:\n    int findMaxK(vector<int>& nums) {\n      int res = -1;\n        std::unordered_set<int> numSet(nums.begin(), nums.end());\n        for (int num : numSet) {\n            if (numSet.find(-num) != numSet.end()) {\n                res = std::max(res, num);\n            }\n        }\n        return res;\n    }\n};\n```\n``` javascript []\nvar findMaxK = function(nums) {\n    let res = -1;\n    let numSet = new Set(nums);\n    for (let num of numSet) {\n        if (numSet.has(-num)) {\n            res = Math.max(res, num);\n        }\n    }\n    return res;\n};\n```
['Python', 'C++', 'Java', 'Python3', 'JavaScript']