# HackerRank – Sock Merchant solution

In this article, I’ll explain how to solve the Sock Merchant algorithm problem on HackerRank.

Problem statement: You’re given an integer array and need to count the number of pairs of matching integers. The integers will be between 1 and 100.

For example, [3,1,3] has one matching pair (the 3s match).

## Approach

First, we know an integer can be paired if it appears in the array twice. For example:

```.wp-block-code {
border: 0;
}

.wp-block-code > span {
display: block;
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```Input = [3,1,3]
Pairs = (3,3)
Count = 1```Code language: plaintext (plaintext)```

Integer 3 appears twice, so it could be paired once. Integer 1 appears once, so it couldn’t be paired.

We can generalize this and say an integer that appears N times results in N / 2 pairs. For example:

``````Input = [3,1,3,3,3,1,2]

3 appears 4 times, resulting in 4 / 2 = 2 pairs.
1 appears 2 times, resulting in 2 / 2 = 1 pair.
2 appears 1 time, resulting in 1 / 2 = 0 pairs.

Count = 3 pairs.```Code language: plaintext (plaintext)```

Note: If an integer appears an odd number of times, integer division takes care of that (dividing two integers in code results in an integer, which chops off decimals. This is why 1 / 2 = 0 in the example above).

This means we don’t need to actually find the pairs. Instead, we’ve reduced the problem to counting the frequency of each integer and dividing the counts by 2. At the end, we can get sum the pair counts to get the total. This can be expressed in the following pseudocode:

``````get counts of each integer
divide each count by 2 to get the number of pairs
sum the pair count
```Code language: plaintext (plaintext)```

I’ll show how to implement this code next.

## Code

Here’s an example of implementing the algorithm to solve the problem (this is using C#):

``````public static int sockMerchant(int n, List<int> ar)
{
int[] intCounts = new int;

foreach(int i in ar)
{
intCounts[i]++;
}

int pairs = 0;
foreach(int count in intCounts)
{
pairs += count / 2;
}

return pairs;
}
```Code language: C# (cs)```

Usually with frequency counting problems, you’d count with a map (Dictionary in C#). But in this problem, we know the range of integers will be between 1 and 100 (according to the problem statement). This means we can use an array instead of a map, which avoids the overhead of using a map. Note: Array size 101 = max value in range (100) + 1, because arrays are 0-based.

This algorithm can be coded in a few different ways, including calculating the pairs while you’re counting. That’s a bit of a micro-optimization though, especially since we know the input size is so small (up to 100 integers). Code readability is more important than a micro-optimization.