Simple Java Solution Using HashMap & Value Count with Comments
\nclass Solution {\n    public int longestPalindrome(String s) {\n        HashMap<Character,Integer> map = new HashMap<>();\n        int n=s.length();\n        
```\nclass Solution {\n    public int longestPalindrome(String s) {\n        HashMap<Character,Integer> map = new HashMap<>();\n        int n=s.length();\n        for(int i=0;i<n;i++){\n            map.put(s.charAt(i),map.getOrDefault(s.charAt(i),0)+1);\n        }\n\t\t//First of all put all the elements in the HashMap\n\t\t//Now there will be two things either the value will be divisible by 2 or not.\n        int result=0;\n        int odd=0;\n        for(int i : map.values()){\n\t\t\t//If it is divisible then we will just add it to the result.\n            if(i%2==0){\n                result+=i;\n            }\n\t\t\t//else we will sub 1 and add it to the answer so that we will always have even values\n\t\t\t/*\n\t\t\tFor example \n\t\t\taaa -> a->3\n\t\t\tif 3 -> we will need only a->2 so we will add value-1\n\t\t\t*/\n            else{\n                result+=i-1;\n                odd=1;\n            }\n        }\n\t\t//why we are using odd?\n\t\t//This is beacause we can have a middle element and it is not neccassary that middle should be present in the string two time\n\t\t/*\n\t\tif aaab\n\t\tso we can have either aaa or aba either way works so just taking only one odd element\n\t\t*/\n        return result+odd;\n    }\n}\n```
['Java']