Counting number of pairs in an integer array So, I wanted to have the count for number of pairs of same integer there are in an array for which I used a HashMap

Input contains two lines:


  
  Integer n:
  
  Signifies the size of input
  Input numbers separated by white space character:
  E.g 4 6 6 7 7 8 7 6 4
  


For the same input,


  Expected Output:
  
  3


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class SockMerchant {

    public int[] returnsockArr() {
        Scanner sc = new Scanner(System.in);
        int arraySize = sc.nextInt();
        int[] sockArr = new int[arraySize];
        for(int i = 0;i &lt;= arraySize-1; i++) {
            sockArr[i] = sc.nextInt();
        }
        sc.close();
        return sockArr;
    }

    public int returnCount(Map&lt;String, Object&gt; mapCount) {
        int count = 0;
        for(String keyName : mapCount.keySet()) {
            int value = (int)mapCount.get(keyName);
            count = count+(value/2);
        }
        return count;
    }

    public int setMapAndReturnCount(int[] sockArr) {
        Map&lt;String, Object&gt; mapCount = new HashMap&lt;String, Object&gt;();
        for(int j = 0;j&lt;= sockArr.length-1;j++) {
            if(mapCount.containsKey(Integer.toString(sockArr[j]))) {
                mapCount.put(Integer.toString(sockArr[j]), (int)mapCount.get(Integer.toString(sockArr[j]))+1);
            }
            else {
                mapCount.put(Integer.toString(sockArr[j]), 1);
            }
        }
        return returnCount(mapCount);
    }

    public static void main(String args[]) {
        SockMerchant sm = new SockMerchant();
        int[] sockArr = sm.returnsockArr();
        int finalCount = sm.setMapAndReturnCount(sockArr);
        System.out.println(finalCount);
    }
}


I think that I have complicated the solution a bit too much

Is there a better approach to do the same thing?
There are couple of improvement possible in your code.

In this method setMapAndReturnCount you should change the declaration of map from,

Map&lt;String, Object&gt; mapCount = new HashMap&lt;String, Object&gt;();


to

Map&lt;Integer, Integer&gt; mapCount = new HashMap&lt;Integer, Integer&gt;();


As you know, you want to store the input array numbers as key in Map and value is the count of occurence of a particular number in the array.

By using Object class as value in Map, you are defeating one of the main purpose of having generics implementation in Java. If you use Object then you need to uselessly cast, and otherwise don't have to.

Another point in same method, you can write your for loop in a better way like this, instead of your current code which does double work of first checking through containsKey and then uses mapCount.get to again pick the value of key.

public int setMapAndReturnCount(int[] sockArr) {
    Map&lt;Integer, Integer&gt; mapCount = new HashMap&lt;Integer, Integer&gt;();
    for (int j = 0; j &lt; sockArr.length; j++) {
        Integer count = mapCount.get(sockArr[j]);
        if (count == null) {
            mapCount.put(sockArr[j], 1);
        } else {
            mapCount.put(sockArr[j], count + 1);
        }
    }
    return returnCount(mapCount);
}


Similarly, you can change returnCount method to this,

public int returnCount(Map&lt;Integer, Integer&gt; mapCount) {
    int count = 0;
    for (Integer value : mapCount.values()) {
        count += (value / 2);
    }
    return count;
}


As you don't need to first iterate on keySet and then retrieve the values with get which will be slower, and instead just iterate it on values as that is what you need for calculating number of pairs, which will be relatively faster.

Also, if you are using Java-8 and above,

You can change your returnCount to one liner like this,

public int returnCount(Map&lt;Integer, Integer&gt; mapCount) {
    return mapCount.values().stream().mapToInt(x -&gt; x / 2).sum();
}