Code issue, , , , , , , , , , , , , , , , , , , , , , ,

my code is give time limit error in 2 test cases can you check or explain why it is give this.

import java.util.*; public class Main { public static void main (String args[]) { Scanner obj = new Scanner(System.in); HashMap<Integer,String> map =new HashMap<>(); int n=obj.nextInt(); for(int i=0; i<n; i++) { int m=obj.nextInt(); if(map.containsKey(m)) { String a=map.get(m); a=a+“a”; map.put(m,a); } else { map.put(m,“a”); } } String str=""; int b=0; Set<Map.Entry<Integer,String>> val = map.entrySet(); for(Map.Entry<Integer,String> vals:val) { String s=vals.getValue(); if(s.length()>str.length()) { b=vals.getKey(); str=s; } } System.out.println(b); } }

@Nitin-Mishra-2380486738834604,
https://ide.codingblocks.com/s/225243 corrected code

Use integer instead of string in the hashmap

but when we take Integer instead of String still its give TLE in 1 test case

@Nitin-Mishra-2380486738834604,
can you share the code you submitted?

import java.util.*; public class Main { public static void main(String args[]) { Scanner obj = new Scanner(System.in); HashMap<Integer, Integer> map = new HashMap<>(); int n = obj.nextInt(); for (int i = 0; i < n; i++) { int m = obj.nextInt(); if (map.containsKey(m)) { map.replace(m, map.get(m) + 1); } else { map.put(m, 1); } } int b = 0; int maxFreq = 0; int ans = 0; Set<Map.Entry<Integer, Integer>> val = map.entrySet(); for (Map.Entry<Integer, Integer> vals : val) { if(vals.getValue()>maxFreq) { maxFreq = vals.getValue(); ans=vals.getKey(); } } System.out.println(ans); } }

@Nitin-Mishra-2380486738834604,

The code is absolutely correct. Can you please refresh your page and submit again?