Sort by

recency

|

6030 Discussions

|

  • + 0 comments

    Here is problem solution in Python, Java, C++, C and Javascript - https://programmingoneonone.com/hackerrank-sales-by-match-problem-solution.html

  • + 0 comments

    Here is my c++ solution , you can find the video explanation here : https://youtu.be/HPIhFXx_DVM

    int sockMerchant(int n, vector<int> ar) {
        map<int, int>mp;
        int result = 0;
        for(int i = 0; i < ar.size(); i++){
            if(mp[ar[i]]){
                result ++;
                mp.erase(ar[i]);
                continue;
            }
            mp[ar[i]]++;
        }
        return result;
    }
    
  • + 0 comments

    C++ solution Using Hashing. int sockMerchant(int n, vector ar) { map count; for(int i = 0; i < ar.size(); i++){ count[ar[i]]++; } int pairs = 0; for(auto p : count){ pairs += p.second/2; } return pairs; }

  • + 0 comments

    python

    def sockMerchant(n, ar):
        # Write your code here
        pairs=0 # intialize pairs
        single_value_ar=set(ar) #create a lsit of unique values
        for sock in single_value_ar: #iterate through the unique values
            pairs+=math.floor(ar.count(sock)/2) #add completed pairs to counter
        return pairs
    
  • + 0 comments

    You don't need to parse the input as it is irrelevant, also that whole LINQ in the C# code gave me a headache:

    List<int> ar = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arTemp => Convert.ToInt32(arTemp)).ToList();
    

    And is totally unnecessary. Treat the second line of input as colours and try to add them in a HashSet. If you can add it, fine continue, if not remove it and increment a counter to count the pairs.

     public static int sockMerchant(int n,string[] ar)
        {
            int pairs = 0;
            HashSet<string> uniqueSocks = new HashSet<string>();
            for(int i =0; i<n; i++){
                if(uniqueSocks.Add(ar[i])){
                    continue;
                }else{
                    uniqueSocks.Remove(ar[i]);
                    pairs++;
                }
            }
            return pairs;
        }