Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
Tournament Winner algorithm javascript using Map?

Hello Tournament Winner is an algorithm challenge where given an array of pairs representing the teams that have competed against each other and an array containing the results of each competition, write a function that returns the winner of the tournament.

Results[i] denote the winner of competitions[i], where 1 in the results array mean the home team won and 0 means the away team won.

This is my code and pseudo-code so far:

function tournamentWinner(competitions, results) {


let currentWinningTeam = "";

  const scoreTracker = new Map();
  scoreTracker.set(currentWinningTeam, 0);
  // since comeptitions and results have same length, use for loop to go through both of the arrays
  // they are in order of results to comeptitionns
  // use hash map to keep track of eachTeams points
  // final loop to find the team with the higest points
  for (const index in competitions) {
    const result = results[index];
    // 0 0 1
    // console.log(result);

    // ["HTML", "C#"] C# > HTML
    const [homeTeam, awayTeam] = competitions[index];

    // console.log(index);
    // #C, Python, Python
    const teamWhoWon = result === 0 ? awayTeam : homeTeam;

    console.log(teamWhoWon)

    updateScores(teamWhoWon, 3, scoreTracker)

    console.log(scoreTracker)
  }
  return currentWinningTeam;
}

function updateScores(teamWhoWon, points, scoreTracker) {
  if (!scoreTracker.has(teamWhoWon)) {
    scoreTracker.set(teamWhoWon, 0)
  } else {
    scoreTracker.set(teamWhoWon, )
  }
}

console.log(
  tournamentWinner(
    [
      ['HTML', '#C'],
      ['#C', 'Python'],
      ['Python', 'HTML'],
    ],
    [0, 0, 1]
  )
);

I am having trouble of how to incrementally increase the points of the team in the scoreTracker hash map in my helper function updateScores()? I am not too familiar with Map methods and can someone show me how I can incrementally increase the points of a team by points?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could get the actual count with Map#get and if falsy, like undefined use zero instead and add one.

m.set(team, (m.get(team) || 0) + 1)

const
    tournamentWinner = (competitions, results) => {
        return [...results.reduce((m, r, i) => 
                (team => m.set(team, (m.get(team) || 0) + 1))
                (competitions[i][+!r]),
            new Map)];
    };

console.log(tournamentWinner([['HTML', '#C'], ['#C', 'Python'], ['Python', 'HTML']], [0, 0, 1]));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!