HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Finding duplicate numbers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
duplicatenumbersfinding

Problem

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 num=new HashMap();
        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}

Solution

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 result = new HashMap();                                                                                                                 

    for(int element : arr)                                                                                                                                                            
      {                                                                                                                                                                               
        if(result.containsKey(element))                                                                                                                                               
          {                                                                                                                                                                           
            result.put(element, result.get(element) + 1);                                                                                                                             
          }                                                                                                                                                                           
        else                                                                                                                                                                          
          {                                                                                                                                                                           
            result.put(element, 1);                                                                                                                                                   
          }                                                                                                                                                                           
      }                                                                                                                                                                               
    System.out.println(result);                                                                                                                                                       
  }

Code Snippets

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);                                                                                                                                                       
  }

Context

StackExchange Code Review Q#33686, answer score: 4

Revisions (0)

No revisions yet.