Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dance2die/077d824e37e9d8618c62786198492afb to your computer and use it in GitHub Desktop.
Save dance2die/077d824e37e9d8618c62786198492afb to your computer and use it in GitHub Desktop.
public static Dictionary<string, List<string>> ConvertToDictionaryOptimized(List<Tuple> actorMovies)
{
var result = new Dictionary<string, List<string>>();
foreach (Tuple actorMovie in actorMovies)
{
if (result.ContainsKey(actorMovie.Actor))
{
var movies = result[actorMovie.Actor];
movies.Add(actorMovie.Movie);
}
else
{
result.Add(actorMovie.Actor, new List<string> {actorMovie.Movie});
}
}
return result;
}
public static Dictionary<string, List<string>> ConvertToDictionary(List<Tuple> actorMovies)
{
var result = new Dictionary<string, List<string>>();
foreach (Tuple actorMovie in actorMovies)
{
if (result.ContainsKey(actorMovie.Actor)) continue;
IEnumerable<Tuple> tempActors = actorMovies.Where(a => a.Actor == actorMovie.Actor);
List<string> movies = tempActors.Select(actor => actor.Movie).ToList();
result.Add(actorMovie.Actor, movies);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment