Sales by Match

Sort by

recency

|

227 Discussions

|

  • + 0 comments
    def sockMerchant(n, ar):
        # Write your code here
        frequency = {}
        result = 0
        for i in ar:
            frequency[i] = frequency.get(i,0) + 1
        for value in frequency.values():
            result += value // 2
        return result
    
  • + 0 comments
    public static void main(String[] args) {
            try (var br = new BufferedReader(new InputStreamReader(System.in));
                 var bw = new BufferedWriter(new OutputStreamWriter(System.out))) {
                int n = Integer.parseInt(br.readLine().trim());
                String[] socks = br.readLine().trim().split(" ");
                int[] sockCounts = new int[101];
                for (String sock : socks) {
                    int color = Integer.parseInt(sock);
                    sockCounts[color]++;
                }
                int pairs = 0;
                for (int count : sockCounts) {
                    pairs += count >> 1;
                }
                bw.write(String.valueOf(pairs));
                bw.newLine();
                bw.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn sales_by_match(n: i32, ar: &[i32]) -> i32 {
        //Time complexity: O(n)
        //Space complexity (ignoring input): O(n)
        let mut socks_hash = std::collections::HashMap::new();
        for sock in ar {
            match socks_hash.get(sock) {
                Some(v) => socks_hash.insert(sock, v + 1),
                None => socks_hash.insert(sock, 1),
            };
        }
        let mut total_pairs = 0;
        for values in socks_hash.values() {
            total_pairs += values / 2;
        }
        total_pairs
    }
    
  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def sales_by_match(n, ar):
        #Time complexity: O(n)
        #Space complexity (ignoring input): O(n)
        socks_dict = {}
        for sock in ar:
            if sock in socks_dict:
                socks_dict[sock] += 1
            else:
                socks_dict[sock] = 1
    
        total_pairs = 0
        for values in socks_dict.values():
            total_pairs += values // 2
    
        return total_pairs
    
  • + 0 comments
    #Python Solution
    def sockMerchant(n, ar):
        pairs = 0
        colors = set(ar)
        
        for color in colors:
            pairs += math.floor(ar.count(color)/2)
        
        return pairs