Drill

ProblemsJavaScript › reporting

Average spend per category

mediumreportingJavaScript

A budgeting view shows what a typical transaction looks like in each category.

averagePerCategory(entries: list<Entry>) → map<string, int>

Solve it in the editor →

Where you start

function averagePerCategory(entries) {
  
}

Worked examples

CallResult
averagePerCategory([{"category":"food","amount":10},{"category":"food","amount":15}]){"food":13}
averagePerCategory([{"category":"rent","amount":3000},{"category":"food","amount":40},{"category":"food","amount":20}]){"rent":3000,"food":30}
averagePerCategory([{"category":"x","amount":0}]){"x":0}
averagePerCategory([]){}

Hint

Collect sums and counts side by side, then divide once at the end. (sum + count / 2) / count rounds half up in integers.

Reference solution in JavaScript
function averagePerCategory(entries) {
  const sums = new Map(), counts = new Map();
  for (const e of entries) {
    sums.set(e.category, (sums.get(e.category) || 0) + e.amount);
    counts.set(e.category, (counts.get(e.category) || 0) + 1);
  }
  const result = {};
  for (const [k, s] of sums) {
    const n = counts.get(k);
    result[k] = Math.floor((s + Math.floor(n / 2)) / n);
  }
  return result;
}

The same problem in another language

More reporting problems in JavaScript