fork(1) download
  1. uint find(T1, T2)(T1 required_element, T2 array) {
  2.  
  3. foreach (i, e; array)
  4. if (required_element == e)
  5. return i;
  6.  
  7. return -1;
  8. }
  9.  
  10. void main() {
  11. import std.stdio;
  12.  
  13. auto a = [1, 5, 9, 43];
  14. auto s = ["cat", "dog", "bar", "foo"];
  15. auto ch = ['a', 'b', 'c', 'd', 'e', 'f'];
  16. auto longDouble = [45.68L, 456.89L, 1.1L, 2.3L, 9.4L, 89.45L];
  17.  
  18. writeln(find(10, a));
  19. writeln(find("foo", s));
  20. writeln(find!(char, char[])('e', ch));
  21. writeln(find(89.45L, longDouble));
  22. }
Success #stdin #stdout 0s 4100KB
stdin
Standard input is empty
stdout
4294967295
3
4
5