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
- Java
- Clojure
- Clojure
- C++
- C#
- Dart
- Elixir
- Fortran
- Go
- Go
- Haskell
- JS
- Pascal
- Perl
- Python
- Ruby
- Rust
- Rust
int i = items.indexOf(x);
int i = -1;
for(int j=0;j<items.length;j++){
if(items[j].equals(x)){
i = j;
break;
}
}
The .equals() method equates objects.
If equating primitives you would use the “x == y” expression
If equating primitives you would use the “x == y” expression
(defn find-index [x items]
(or (->> items
(map-indexed vector)
(filter #(= x (peek %)))
ffirst)
-1))
(defn find-index [x items]
(reduce-kv
#(if (= x %3) (reduced %2) %1) -1 items))
Clojure already provides some iteration functions to be used with the concept of reduction
auto it = std::find(items.begin(), items.end(), x);
i = it == items.end() ? std::distance(items.begin(), it) : -1;
i=items.indexOf(x)
i = Enum.find_index(items, & &1 == x)
i = findloc(items, x)
if (i == 0) i = -1
The -1 part is actually unnecessary in Fortran, where array indices start at 1.
i := -1
for j, e := range items {
if e == x {
i = j
break
}
}
Explicit loop, for your own strongly typed helper func.
let i = items.indexOf(x);
i := Items.IndexOf(x);
$i = -1;
$index = 0;
foreach (@items) {
$i = $index, last if $x eq $items[$index];
$index++;
}
i = items.index(x) if x in items else -1
i = items.index(x) || -1
.index() is not in Enumerable so use a supported data structure like Array