DEV Community

Discussion on: Daily Challenge #172 - Find All in an Array

Collapse
 
jessekphillips profile image
Jesse Phillips
// D
pure nothrow @safe
auto findIndex(const(int)[] arr, size_t needle) {
  return arr.enumerate
     .filter!(x => x[1] == needle) 
     .map!(x => x[0])
     //.array
     ;
} unittest {
  assert(findIndex([6, 9, 3, 4, 3, 82, 11], 3).equal([2, 4]));
} 

The commented out array line would allocate a new array to meet the specification.