I have a dictionary. The same instance of an object can spread across multiple keys. I am writing a method so that you can pass the instance, and all keys with that instance will be removed.
The following works, but I am weak with Linq. Is there a better approach to this? I am interested in elegant code, but I am moreso focused on speed. How can I make this code run faster (without removing whether or not the removal was successful)?
/// <summary>
/// Removes obstacle from the level, and returns true if the removal was successful
/// </summary>
public bool Remove(Obstacle obstacle)
{
if (!obstacleMap.ContainsValue(obstacle))
{
return false;
}
foreach (var key in obstacleMap.Keys.Where(k => obstacleMap[k] == obstacle))
{
obstacleMap.Remove(key);
}
return true;
}