Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Java
- Clojure
- C++
- C#
- D
- Dart
- Elixir
- Go
- Go
- Haskell
- JS
- JS
- Kotlin
- PHP
- Pascal
- Perl
- Python
- Python
- Python
- Ruby
- Rust
- Scala
- Smalltalk
std::find_if(m.begin(), m.end(), [v](const auto& mo) {return mo.second == v; }) != m.end();
c++ 14
A lambda expression is used because built-in functions would search by key, not by value.
A lambda expression is used because built-in functions would search by key, not by value.
func containsValue[M ~map[K]V, K, V comparable](m M, v V) bool {
for _, x := range m {
if x == v {
return true
}
}
return false
}
This generic function works for any type parameters K, V that are comparable
func containsValue(m map[K]T, v T) bool {
for _, x := range m {
if x == v {
return true
}
}
return false
}
You have to iterate explicitly. In this implementation, the types K, T are not generic.
elem v (elems m)
Object.values(m).includes(v)
JavaScript objects are hashmaps.
Object.values() converts a hashmap to a list of values.
Array#includes then checks whether v is included.
Object.values() converts a hashmap to a list of values.
Array#includes then checks whether v is included.
[...m.values()].includes(v)
Unlike the previous implementation, this works for Map objects rather than normal Objects.
print "Found it!" if exists $m{$v};
v in m.values()
def k(x, m):
for k, v in m.items():
if v == x: return k
k = k(v, m)
x = False
for y in m.items():
if y[1] == v:
x = y[0]
break
m.valuesIterator.contains(v)
m includes: v.