Simple java one pass HashSet solution
\n# Approach\n# Explanation of findMaxK Method\nThis method aims to find the maximum value k such that both k and -k exist in the given array nums. It returns k
\n# Approach\n# Explanation of findMaxK Method\nThis method aims to find the maximum value k such that both k and -k exist in the given array nums. It returns k, or -1 if no such k exists.\n\n# Algorithm Overview\n1. Initialize a HashSet set to store encountered numbers and a variable maxNum to track the maximum value of k.\n2. Iterate through each element num in the array nums.\n3. Check if the negation of num exists in the set. If it does, update maxNum to the maximum of its current value and the absolute value of num.\n4. Add num to the set.\n5. After iterating through all elements, return maxNum.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\n    public int findMaxK(int[] nums) {\n        Set<Integer> set = new HashSet<>();\n        int maxNum = -1;\n\n        for(int num : nums){\n            if(set.contains(-num))\n                maxNum = Math.max(maxNum, Math.abs(num));\n                \n            set.add(num);\n        }\n\n        return maxNum;\n    }\n}\n```
['Java']