Sort by

recency

|

6054 Discussions

|

  • + 0 comments

    java 8 Collections.sort(ar); int count = 0; int temp = 0; for(int i = 0; i < n; i++){

            if(temp == ar.get(i)){
                temp = 0;
                continue;
            }
            temp = ar.get(i);
            if((i + 1) < n && temp == ar.get(i + 1)){
                count++;
            }
        }
        return count;
    
  • + 0 comments

    Using Java stream

        public static int sockMerchant(int n, List<Integer> ar){
    
            Map<Integer, Integer> map = ar.stream().map(i -> new AbstractMap.SimpleEntry<>(i, 1)).collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey,
                    AbstractMap.SimpleEntry::getValue,
                    Integer::sum));
            return map.values().stream().mapToInt(integer -> integer / 2).sum();
        }
    
  • + 0 comments

    in c#

    int pairs = 0;
            var colors = ar.Distinct();
            foreach(var a in colors){
                var pair = ar.Where(x => x == a).ToList();
                if(pair.Count() % 2 != 0) pair.Remove(a);
                pairs += pair.Count()/2;
            }
            return pairs;
    
  • + 0 comments

    Python

    def sockMerchant(n, ar):
        seen = set()
        pairs = 0
        for c in ar:
            if c not in seen:
                seen.add(c)
            else:
                pairs +=1
                seen.remove(c)
        return pairs
    
  • + 0 comments

    public static int sockMerchant(int n, List ar) { int pairCount = 0; Dictionary DictionaryObj = new Dictionary();

        foreach (var item in ar)
        {
            if (DictionaryObj.ContainsKey(item))
            {
                DictionaryObj[item]++;
            }
            else
            {
                DictionaryObj[item] = 1;
            }
        }
    
        foreach (int count in DictionaryObj.Values)
        {
            pairCount += count / 2;
        }
        return pairCount;
    }****