• + 8 comments

    Hello friends,

    In this video, I have explained sock merchant hackerrank solution algorithm. hackerrank sock merchant problem can be solved by using set data structure. The complexity of sock merchant hackerrank solution is O (n).

    If interested to know more about the generic algorithm in details-

    click here for the video explanation of generic algorithm with complexity analysis.

    or you can click on the image too to follow youtube tutorial.

    Here is the working solution:-

    source code :

    static int sockMerchant(int n, int[] ar) {
    		HashSet<Integer> set = new HashSet<Integer>();
    		int count = 0;
    		for (int i = 0; i < n; i++) {
    			int element = ar[i];
    			if (set.contains(element)) {
    				set.remove(element);
    				count++;
    			} else {
    				set.add(element);
    			}
    
    		}
    		return count;
    
    	}
    

    Would really appreciate your feedback like, dislike , comment etc. on my video.

    Do not forget to upvote, if you find it useful.