Arrays.sort(arr); if(arr.length==1) return 2; int x=arr[arr.length-1]; int dummy[]=new int[x+1]; for(Integer z: arr){ dummy[z]=1; } int found=-1; for(int i=1;i<=x;i++){ if(dummy[i]==0){ found= i; } } if(found==-1) return x+1; else return found; https://www.geeksforgeeks.org/problems/find-duplicates-in-an-array/1 1 min Vineet Kumar to You (direct message) 20:34 ok sir ===================================================== //{ Driver Code Starts import java.io.*; import java.util.*; class IntArray { public static int[] input(BufferedReader br, int n) throws IOException { String[] s = br.readLine().trim().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]); return a; } public static void print(int[] a) { for (int e : a) System.out.print(e + " "); System.out.println(); } public static void print(ArrayList a) { for (int e : a) System.out.print(e + " "); System.out.println(); } } class GFG { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t; t = Integer.parseInt(br.readLine()); while (t-- > 0) { int n; n = Integer.parseInt(br.readLine()); int[] arr = IntArray.input(br, n); Solution obj = new Solution(); ArrayList res = obj.duplicates(arr); IntArray.print(res); } } } // } Driver Code Ends https://www.geeksforgeeks.org/problems/find-duplicates-in-an-array/1 class Solution { public static ArrayList duplicates(int[] arr) { // code here //2 3 1 HashMap hm=new HashMap<>(); //put , get , ContainsKey //process each and every element //check wheather the element present in Map or not //if yes get actual freq+1 //if no just place aginest 1 for(Integer i: arr){ if(hm.containsKey(i)){ int actfreq=hm.get(i); actfreq++; hm.put(i,actfreq); }else{ hm.put(i,1); } } ArrayList al=new ArrayList<>(); for(Map.Entry me: hm.entrySet()){ if(me.getValue()>1){ al.add(me.getKey()); } } if(al.size()==0) { al.add(-1); return al; } else{ Collections.sort(al); return al; } } } =========================================================== https://www.geeksforgeeks.org/problems/find-the-frequency/1 //{ Driver Code Starts //Initial Template for Java import java.io.*; import java.util.*; class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } String next(){ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch (IOException e){ e.printStackTrace(); } } return st.nextToken(); } String nextLine(){ String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } Integer nextInt(){ return Integer.parseInt(next()); } } class GFG { public static void main(String args[]) throws IOException { FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-- > 0) { Solution ob = new Solution(); int N = sc.nextInt(), Arr[] = new int[N]; for(int i = 0;i hm=new HashMap<>(); //put , get , ContainsKey //process each and every element //check wheather the element present in Map or not //if yes get actual freq+1 //if no just place aginest 1 for(Integer i: arr){ if(hm.containsKey(i)){ int actfreq=hm.get(i); actfreq++; hm.put(i,actfreq); }else{ hm.put(i,1); } } int x=0; for(Map.Entry me: hm.entrySet()){ if(me.getKey()==X){ x= me.getValue(); } } return x; } } ============================================= class Solution { // Function to return the position of the first repeating element. public static int firstRepeated(int[] arr) { // Your code here HashMap hm=new HashMap<>(); int x=-1; for(int i=arr.length-1;i>=0;i--){ if(hm.containsKey(arr[i])){ x=i+1; }else{ hm.put(arr[i],1); } } return x; } }