I have written this code which finds duplicate numbers and the occurrence of a particular number in an array. I have used a HashMap, but I want to know if there is a more efficient way to do the same, or whether I should be using another method.import java.util.*;class test7{    public static void main(String ...a)    {        int []arr={10,20,10,2,11,10,32,15,15,10,10};        HashMap<Integer,Integer> num=new HashMap<Integer,Integer>();        for(int t: arr)         {                   Integer tmp_int=new Integer(t);            if(num.containsKey(tmp_int))            {                   Integer i_ob=num.get(tmp_int);                num.put(tmp_int,new Integer(i_ob.intValue()+1));            }            else            {                num.put(tmp_int,new Integer(1));            }        }        System.out.println(num);    }   }Output:{32=1, 2=1, 20=1, 10=5, 11=1, 15=2}
Finding duplicate numbers
java
I have modified your code. The logic is same as you have done. You do not need to create those Integer. Read about Autoboxing. If the input array is large, you will see performance hit for creating Integer. Note that Integer is a class, and not a primitive type in java.public static void main(String[] args) {                                                                                                                                                int[] arr={10,20,10,2,11,10,32,15,15,10,10};                                                                                                                                          HashMap<Integer,Integer> result = new HashMap<Integer,Integer>();                                                                                                                     for(int element : arr)                                                                                                                                                                  {                                                                                                                                                                                       if(result.containsKey(element))                                                                                                                                                         {                                                                                                                                                                                       result.put(element, result.get(element) + 1);                                                                                                                                       }                                                                                                                                                                                   else                                                                                                                                                                                    {                                                                                                                                                                                       result.put(element, 1);                                                                                                                                                             }                                                                                                                                                                                 }                                                                                                                                                                                   System.out.println(result);                                                                                                                                                         }