Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #52 Check if map contains value

Determine whether map m contains an entry with value v, for some key.

Illustration
uses fgl;
m.IndexOfData(v) >= 0
#include <algorithm>
std::find_if(m.begin(), m.end(), [v](const auto& mo) {return mo.second == v; }) != m.end();
using System.Collections.Generic;
m.ContainsValue(v)
import std.algorithm;
m.byValue.canFind(v);
m.containsValue(v);
Map.values(m) |> Enum.member?(v)
func containsValue(m map[K]T, v T) bool {
	for _, x := range m {
		if x == v {
			return true
		}
	}
	return false
}
elem v (elems m)
Object.values(m).includes(v)
[...m.values()].includes(v)
import java.util.Map;
m.containsValue(v)
in_array($v, $m, true);
print "Found it!" if exists $m{$v};
v in m.values()
m.value?(v)
use std::collections::BTreeMap;
let does_contain = m.values()
        .any(|&val| *val == v);
!m.values.find(_ == v).isEmpty()

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms