Skip to main content
added 52 characters in body
Source Link
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65

Don't test whether a HashSet contains a key if you want to remove it.
Instead, just straight try to remove it, halving your work.:

if(list.Contains(value)) {
    list.Remove(value);
}

BecomesInstead, just straight try to remove it:

list.Remove(value);

The replacement is cleaner, shorter, and more efficient. What's not to like?

Don't test whether a HashSet contains a key if you want to remove it.
Instead, just straight try to remove it, halving your work.

if(list.Contains(value)) {
    list.Remove(value);
}

Becomes

list.Remove(value);

Don't test whether a HashSet contains a key if you want to remove it:

if(list.Contains(value)) {
    list.Remove(value);
}

Instead, just straight try to remove it:

list.Remove(value);

The replacement is cleaner, shorter, and more efficient. What's not to like?

quoted OP's code
Source Link
t3chb0t
  • 44.7k
  • 9
  • 84
  • 191

Don't test whether a HashSet contains a key if you want to remove it.
Instead, just straight try to remove it, halving your work.

if(list.Contains(value)) {
    list.Remove(value);
}
if(list.Contains(value)) {
    list.Remove(value);
}

Becomes

list.Remove(value);

Don't test whether a HashSet contains a key if you want to remove it.
Instead, just straight try to remove it, halving your work.

if(list.Contains(value)) {
    list.Remove(value);
}

Becomes

list.Remove(value);

Don't test whether a HashSet contains a key if you want to remove it.
Instead, just straight try to remove it, halving your work.

if(list.Contains(value)) {
    list.Remove(value);
}

Becomes

list.Remove(value);
Source Link
Deduplicator
  • 19.9k
  • 1
  • 32
  • 65

Don't test whether a HashSet contains a key if you want to remove it.
Instead, just straight try to remove it, halving your work.

if(list.Contains(value)) {
    list.Remove(value);
}

Becomes

list.Remove(value);