Answers for "Complete the sock Merchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available."

0

Complete the sock Merchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.

function interview(n, arr) {
    let count = 0;
    const socks = {};

    for (let i of arr) {
        if (socks[i]) {
            socks[i] = false;
            count++
        } else {
            socks[i] = true
        }
    }
    return count
}


console.log(interview(9, [10,20,20,10,10,30,50,10,20]))
console.log(interview(100, [44,55,11,15,4,72,26,91,80,14,43,78,70,75,36,83,78,91,17,17,54,65,60,21,58,98,87,45,75,97,81,18,51,43,84,54,66,10,44,45,23,38,22,44,65,9,78,42,100,94,58,5,11,69,26,20,19,64,64,93,60,96,10,10,39,94,15,4,3,10,1,77,48,74,20,12,83,97,5,82,43,15,86,5,35,63,24,53,27,87,45,38,34,7,48,24,100,14,80,54]))
console.log(interview(100,[50,49,38,49,78,36,25,96,10,67,78,58,98,8,53,1,4,7,29,6,59,93,74,3,67,47,12,85,84,40,81,85,89,70,33,66,6,9,13,67,75,42,24,73,49,28,25,5,86,53,10,44,45,35,47,11,81,10,47,16,49,79,52,89,100,36,6,57,96,18,23,71,11,99,95,12,78,19,16,64,23,77,7,19,11,5,81,43,14,27,11,63,57,62,3,56,50,9,13,45]))
Posted by: Guest on October-28-2020
0

Complete the sock Merchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.

fun main() {
    val colors = arrayOf(10, 20, 20, 10, 10, 30, 50, 10, 20)
    var pairs = 0
    val colorFrequencies: MutableMap<Int, Int> = mutableMapOf()
    colors.forEach { color ->
        val count = colorFrequencies.getOrDefault(color, 0)
        colorFrequencies[color] = count+1
    }
    colorFrequencies.forEach { entry ->
        pairs += entry.value / 2
    }
    println(pairs)
}
Posted by: Guest on October-28-2020
0

Complete the sock Merchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.

function sockMerchant(n, array) {
  let count = 0;
  const seen = {};
  array.forEach((item) => {
    if (seen[item]) { // one sock of this color was seen before, so this completes a pair
      seen[item] = false;
      count++;
    } else { // this is the one in a pair
      seen[item] = true;
    }
  });
  return count;
}
Posted by: Guest on October-28-2020
0

Complete the sock Merchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.

function sockMerchant(n, ar) {
  //Need to initiate a count variable to count pairs and return the value
  let count = 0
  //sort the given array
  ar = ar.sort()
  //loop through the sorted array 
  for (let i=0; i < n-1; i++) {
    //if the current item equals to the next item 
    if(ar[i] === ar[i+1]){
      //then that's a pair, increment our count variable
      count++
      //also increment i to skip the next item
      i+=1
    }
  }
  //return the count value
  return count
}
Posted by: Guest on October-28-2020
0

Complete the sock Merchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available.

int sockMerchant(int n, vector<int> ar) {
unordered_map<int,int> count;
for(int i=0;i<ar.size();i++){
count[ar[i]]++;
}
int result = 0;
for(auto it=count.begin();it!=count.end();it++){
result += (it->second/2);
}
return result;
}
Posted by: Guest on October-28-2020

Code answers related to "Complete the sock Merchant function in the editor below. It must return an integer representing the number of matching pairs of socks that are available."

Browse Popular Code Answers by Language