0

I have a situation in which I have a list of 150 players(Names, id(uuid),team names etc.).

  1. The user can make a team of 10 players and he can create as many teams as possible.
  2. I need to make sure that the user does not try to save the same team twice i.e. same set of players in different order.

I want to achieve it in the most efficient way possible. I was thinking about a hashing function that could solve my problem. I haven't figured it out yet but I just wanted to know what do you guys think? I have to implement this in nodejs if it helps anyway.

tumulr
  • 133
  • 4
  • 11
  • possible duplicate http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – phenxd Feb 18 '16 at 21:17
  • Sometimes I feel like it is better to code something that works, and then see if it sould be improved. I feel like this is one of those cases. – phenxd Feb 18 '16 at 21:18

1 Answers1

0

When the user makes a team, sort the list of names and join them together with list.join().

Use the resulting string as a key to detect duplicate teams, like this:

var check={};

function submit(team /*array*/) {
    var key = team.sort().join();
    if (check[key]) {
        //... error, duplicate ...
    }
    check[key]=true;
    //... continue with submission...

This is probably the fastest way in JavaScript

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87