🔥 4 lines of simple code 3 approaches fully explained ✅|| Easy to understand👍 ||  Beat 96% users🚀
Intuition\n# Brute Force :\nPython3 []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        c=-1\n        for i in nums:\n            i=i*
# Intuition\n# Brute Force :\n```Python3 []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        c=-1\n        for i in nums:\n            i=i*-1\n            if i in set(nums) and i>c:   c=i\n        return c\n```\n```Python []\nclass Solution(object):\n    def findMaxK(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: int\n        """\n        c=-1\n        for i in nums:\n            i=i*-1\n            if i in set(nums) and i>c:   c=i\n        return c\n```\n```C++ []\nclass Solution {\npublic:\n    int findMaxK(vector<int>& nums) {\n        int c=-1;\n        for(int i=0;i<nums.size();i++){\n            int n=(nums[i]*-1);\n            if((find(nums.begin(),nums.end(),n)!=nums.end()) && n>c){\n                c=n;\n            }\n        }\n        return c;\n    }\n};\n```\n**Python, Python3 and C++ :**\n1. **Initialization :** The variable **\'c\'** is initialized to -1.\n2. **Iteration through the list nums :** The code iterates through each element **\'i\'** in the input list **\'nums\'**.\n3. **Negating elements :** Each element **nums[i]** is negated by multiplying it by -1 **(int n = (nums[i] * -1))**. This effectively changes each element to its negative counterpart(simply changes positive number to negative and negative number to positive).\n4. **Checking existence and updating c:** It checks if the negated element **i** exists in the original list **nums** using a set to perform a membership test **(i in set(nums))** in python and **(find(nums.begin(), nums.end(), n) != nums.end())** in c++ . Additionally, it checks if the negated element is greater than the current maximum integer **c**. If both conditions are met, **c** is updated to the current negated element **i**.\n5. Finally return c.\n\n**There is a another brute force method which reduce the time complexity to O(n)**\n```Python3 []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        c=-1\n        nums=set(nums)\n        for i in nums:\n            i=i*-1\n            if i in nums and i>c:   c=i\n        return c\n```\n```Python []\nclass Solution(object):\n    def findMaxK(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: int\n        """\n        c=-1\n        nums=set(nums)\n        for i in nums:\n            i=i*-1\n            if i in nums and i>c:   c=i\n        return c\n```\n```C++ []\nclass Solution {\npublic:\n    int findMaxK(vector<int>& nums) {\n        int c=-1;\n        unordered_set<int> s(nums.begin(),nums.end());\n        for(int i:s){\n            int n=(i*-1);\n            if((find(s.begin(),s.end(),n)!=s.end()) && n>c){\n                c=n;\n            }\n        }\n        return c;\n    }\n};\n```\nIn above code we just convert the **nums** to the **set** so the **in** and **find** operations takes O(1) time complexity in **set** so overall time complexity will be O(n).\n\n\n# Hashing or Counting Approach :\n```Python3 []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        c=-1\n        nums=list(set(nums))\n        for i in range(len(nums)):\n            if nums[i]<0:\n                nums[i]=nums[i]*-1\n        co=Counter(nums)\n        for i,j in co.items():\n            if j==2 and i>c:\n                c=i\n        return c\n```\n```Python []\nclass Solution(object):\n    def findMaxK(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: int\n        """\n        c=-1\n        nums=list(set(nums))\n        for i in range(len(nums)):\n            if nums[i]<0:\n                nums[i]=nums[i]*-1\n        co=Counter(nums)\n        for i,j in co.items():\n            if j==2 and i>c:\n                c=i\n        return c\n```\n```C++ []\nclass Solution {\npublic:\n    int findMaxK(vector<int>& nums) {\n        set<int> s(nums.begin(),nums.end());\n        vector<int> num(s.begin(),s.end());\n        int c=-1;\n        for(int i=0; i<num.size(); i++){\n            if (num[i]<0){\n                num[i]*=-1;\n            }\n        }\n        map<int,int> m;\n        for(int i:num){\n            m[i]++;\n        }\n        for(auto i:m){\n            if(i.second>1 && i.first>c){\n                c=i.first;\n            }\n        }\n        return c;\n    }\n};\n```\n**Python and Python3 :**\n1. **Initialization :** Variable **c** is initialized to -1.\n2. **Removing Duplicates :** The list **nums** is converted to a set to remove duplicates, then converted back to a list. This ensures that each unique element appears only once in the list.\n3. **Negating Elements :** The code iterates through the indices of the list **nums**. If the element at index **i** is negative, it is negated (multiplied by -1).Simply it converts the whole **nums** elements into positive integers.\n4. **Counting Frequencies :** A Counter object **co** is created to count the frequency of each element in the modified **nums** list.\n5. **Finding the Maximum :** The code iterates through the items of the Counter object. If the frequency of an element is 2 (indicating it occurs twice) and the element is greater than the current maximum integer **c**, **c** is updated to that element.\n6. Finally return **c**.\n\n**C++ :**\n1. **Set Creation :** It creates a set **s** from the input vector **nums** to remove duplicates and store unique elements.\n2. **Vector Conversion :** It converts the set **s** back to a vector **num**. This conversion ensures that **num** contains only unique elements from **nums**.\n3. **Initialization :** It initializes **c** to -1.\n4. **Negating Elements :** It iterates through the vector **num** and negates negative elements by multiplying them by -1.\n5. **Map Creation and Frequency Counting :** It creates a map **m** to count the frequency of elements in **num**. It then iterates through **num** and updates the count of each element in **m**.\n6. **Finding the Maximum :** It iterates through the map **m** and find If an element occurs more than once **(i.second > 1)** and is greater than the current maximum integer **c**, **c** is updated to that element.\n7. Finally return **c**.\n\n# Sorting :\n \n```Python3 []\nclass Solution:\n    def findMaxK(self, nums: List[int]) -> int:\n        c=-1\n        nums=list(set(nums))\n        for i in range(len(nums)):\n            if nums[i]<0:\n                nums[i]=nums[i]*-1\n        nums.sort()\n        for i in range(len(nums)-1):\n            if nums[i]==nums[i+1] and nums[i]>c:\n                c=nums[i]\n        return c\n```\n```Python []\nclass Solution(object):\n    def findMaxK(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: int\n        """\n        c=-1\n        nums=list(set(nums))\n        for i in range(len(nums)):\n            if nums[i]<0:\n                nums[i]=nums[i]*-1\n        nums.sort()\n        for i in range(len(nums)-1):\n            if nums[i]==nums[i+1] and nums[i]>c:\n                c=nums[i]\n        return c\n```\n```C++ []\nclass Solution {\npublic:\n    int findMaxK(vector<int>& nums) {\n        set<int> s(nums.begin(),nums.end());\n        vector<int> num(s.begin(),s.end());\n        int c=-1;\n        for(int i=0; i<num.size(); i++){\n            if (num[i]<0){\n                num[i]*=-1;\n            }\n        }\n        sort(num.begin(),num.end());\n        for(int i=0;i<num.size()-1;i++){\n            if(num[i]==num[i+1] && num[i]>c){\n                c=num[i];\n            }\n        }\n        return c;\n    }\n};\n```\n**Python, Python3 and C++ :**\n1. **Initialization :** Variable **c** is initialized to -1.\n2. **Removing Duplicates :** The list **nums** is converted to a set to remove duplicates, then converted back to a list. This ensures that each unique element appears only once in the list.\n3. **Negating Elements :** The code iterates through the indices of the list **nums**. If the element at index **i** is negative, it is negated (multiplied by -1).\n4. **Sorting :** The list **nums** is sorted in ascending order using the sort() method.\n5. **Finding Maximum :** The code iterates through the indices of the sorted list **nums** up to the second last element. If the current element is equal to the next element(by sorting the elements the elements which is twice in numbers are come next to each other so it helps to check same elements easily) and greater than **c**, **c** is updated to the current element.\n6. Finally return **c**.\n\n\n# Approach : Brute Force, Hashing or Counting and Sorting\n\n# Complexity\n- Time complexity: O(n) in Counting, O(nlogn) in Sorting and O(n^2) and O(n) in Brute Force\n\n- Space complexity: O(1) and O(n) in Brute Force, O(m) in Sorting and Counting where m is the unique elements in **\'nums\'** vector.\n\nI hope you understand the code \uD83D\uDE4F and if you have any query let me know in the comment and tell me which approach you like the most.\nFinally Thanks you all for visiting here.
['Array', 'Hash Table', 'Two Pointers', 'Ordered Map', 'Sorting', 'Python', 'C++', 'Python3']