Sales by Match

Sort by

recency

|

345 Discussions

|

  • + 0 comments

    Kotlin Solution

    fun sockMerchant(n: Int, ar: Array<Int>): Int {
            var numberOfPairs = 0
            ar.groupBy { it }.forEach {
                numberOfPairs += it.value.size / 2
            }
            return numberOfPairs
        }
    
  • + 0 comments

    python without build in function (Manual Way with Logic)

    def isExistCompare(compare,record):
        for data in compare:
            if record == data:
                return True
        return False
    
    def getCompare(arr):
        compare = []
        for idx in range(len(arr)):
            record = arr[idx]
            if idx == len(arr)-1:
                checkAr = isExistCompare(compare, record)
                if  checkAr:
                    compare.append(record)
                break
            recordNext = arr[idx+1]
            if record == recordNext:
                compare.append(record)
                continue
            checkAr = isExistCompare(compare, record)
            if  checkAr:
                compare.append(record)
            
        return compare
        
    def coupleMapping(comparedData):
        res = []
        counter = 1
        for idx in range(len(comparedData)):
            records = comparedData[idx]
            if idx == len(comparedData)-1:
                if records == comparedData[len(comparedData)-2]:
                    counter +=1
                    res.append({"count": counter-1, "records":records })
                break
            else:
                recordsNext = comparedData[idx+1]
                if records == recordsNext:
                    counter+=1
                else:
                    res.append({"count": counter, "records":records })
                    counter = 1
        return res
    
    def coupleCompare(mapping):
        couple = 0
        for idx in range(len(mapping)):
            record = mapping[idx]
            if record["count"] %2 ==0 :
                couple += int(record["count"]/2)
            else:
                couple += int((record["count"]-1)/2)
        return couple
                   
    def sockMerchant(n, ar):
        arr = sorted(ar)
        comparedData = getCompare(arr)
        mapping = coupleMapping(comparedData)
        couple = coupleCompare(mapping)
        return couple
        
    
  • + 0 comments

    In Js:

    function sockMerchant(n, ar) {
        // Write your code here
        let duplicate = [];
        let sar = ar.sort((a, b) => a- b);
        for(let i=0; i<n; i++){
            if(sar[i] === sar[i+1]){
                duplicate.push(sar[i]);
                ++i;
            }
        }
        return duplicate.length;
    }
    
  • + 0 comments

    py def sockMerchant(n, ar): store = [] result = 0 for i in ar: if i not in store: store.append(i) else: store.remove(i) result += 1

    return result
    
  • + 0 comments

    Scala

    def sockMerchant(n: Int, ar: Array[Int]): Int = {
            val distinctSocks = ar.distinct
            
            var pairs: Int = 0
            for(i <- distinctSocks) {
                pairs+= (ar.count(_==i))/2
            }
            pairs
        }