Can we solve this Sock Merchant problem in less complexity?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
The Sock Merchant problem, a classic from algorithm challenges, invites us to determine the number of matching pairs of socks from a given collection. It's a nuanced problem that offers deeper insight into efficient algorithm design. In this article, we will delve into the complexities, explore potential optimization strategies, and evaluate the feasible approaches to solving this problem with minimal computational overhead.
Problem Definition
The Sock Merchant problem can be formally defined as follows:
Given an array, `ar`, of integers representing the colors of socks, and an integer, `n`, representing the total number of socks, find the number of pairs of socks with matching colors.
Example
For instance, if the input array `ar` is `[10, 20, 20, 10, 10, 30, 50, 10, 20]`, the output should be `3`, since there are:
- Two pairs of color `10`,
- One pair of color `20`,
- And one unpaired sock of colors `10`, `30`, `50`.
Initial Analysis
The brute force approach involves a nested loop where each sock is compared with every other sock, yielding a time complexity of . It is apparent that this method isn't scalable for larger datasets, pushing us towards more optimized solutions.
Optimal Solution
To solve the sock matching problem efficiently, we can utilize a hash table (or dictionary in Python), which will store the frequency of each color. This approach allows us to achieve a linear time complexity, , with a constant space complexity for the hash table.
Steps
- Initialize an empty dictionary to count occurrences of each sock color.
- Traverse through the sock array:
- For each sock, increment its count in the dictionary.
- Calculate the total number of pairs:
- For each entry in the dictionary, use integer division to determine the number of pairs for that color.
- Sum up all pairs to get the final result.
Complexity Analysis
- Time Complexity: , because we make one pass through the sock list.
- Space Complexity: , where is the number of distinct sock colors.
Implementation Example in Python
- Trade-offs: The choice of using a hash table improves time efficiency but costs extra space. However, this trade-off is beneficial when is large.
- Edge Cases: Consider cases with no socks or all socks of the same color to ensure the solution's robustness.
- Parallel Processing: For extremely large datasets, further optimizations can incorporate parallel processing to distribute the count operation across multiple processes, provided hardware capabilities support it.
Related reading
- Can we use binary search tree to simulate heap operation?
- Can we use Morris Traversal for postorder?
- Can we use Normal Equation for Logistic Regression ?
- Candidate Elimination Algorithm
- Cancelling a long running regex match?
- Capturing self weak or unowned on asynchronous network requests
- Candidate Elimination Algorithm
- Cannot understand this prime generator algorithm in my textbook

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.