Sales by Match

Sort by

recency

|

230 Discussions

|

  • + 0 comments

    C#

    public static int sockMerchant(int n, List<int> ar)
        {
            var orderArray = ar.Order().ToList();
            
            var sets = orderArray.GroupBy(n => n).ToList();
            
            int countPairs = 0;
            
            sets.ForEach(s => countPairs += s.ToList().Count() / 2);
            
            return countPairs;
        }
    
  • + 0 comments

    my python 3 def sockMerchant(n, ar):

    unpaired, pairs = set(), 0
    for i in ar:
        if i in unpaired:
            pairs += 1
            unpaired -= {i}
            continue
        unpaired |= {i}
    return pairs
    
  • + 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();
            }
        }