Sales by Match

Sort by

recency

|

228 Discussions

|

  • + 0 comments
    public static int sockMerchant(int n, List<Integer> ar) {
    // Write your code here
        int result = 0;
        Map<Integer, Integer> countMap = new HashMap<>();
        for(Integer i : ar){
            countMap.put(i, countMap.getOrDefault(i, 0) + 1);
        }
        for(Map.Entry<Integer, Integer> entry : countMap.entrySet()){
            result = result + entry.getValue() / 2;
        }
        return result;
    }
    
  • + 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