Drill

ProblemsC# › games

Score dice pairs

hardgamesHash mapsMathArraysC#

A dice game awards points from matching pairs. For each face value that appears more than once, every pair of matching dice contributes the face value to the total score.

DicePairsScore(dice: list<int>) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int DicePairsScore(List<int> dice) {
    
}

Worked examples

CallResult
DicePairsScore(new List<int> { 2, 3, 2, 5, 3 })5
DicePairsScore(new List<int> { 6, 6, 6 })18
DicePairsScore(new List<int> { 1, 2, 3 })0
DicePairsScore(new List<int> { 4, 4, 4, 4 })24

Hint

Count frequencies, then for each face with two or more occurrences add value * count * (count - 1) / 2.

Reference solution in C#
public int DicePairsScore(List<int> dice) {
    var freq = new Dictionary<int, int>();
    foreach (int d in dice) {
        if (freq.ContainsKey(d)) freq[d]++;
        else freq[d] = 1;
    }
    int sum = 0;
    foreach (var kv in freq) {
        int k = kv.Value;
        if (k >= 2) sum += kv.Key * k * (k - 1) / 2;
    }
    return sum;
}

The same problem in another language

More games problems in C#