DEV Community

loading...

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

Collapse
exts profile image
Lamonte

In Dart:

void main() {
  print(findAll([6, 9, 3, 4, 3, 82, 11], 3));
  print(findAll([10, 16, 20, 6, 14, 11, 20, 2, 17, 16, 14], 16));
  print(findAll([20, 20, 10, 13, 15, 2, 7, 2, 20, 3, 18, 2, 3, 2, 16, 10, 9, 9, 7, 5, 15, 5], 20));
}

List<int> findAll(List<int> items, int find) {
  var found = List<int>();
  for(var idx = 0; idx < items.length; idx++) {
    if(items[idx] == find) {
      found.add(idx);
    }
  }
  return found;
}

// alternative approach, which cuts it down maybe a line? lol
List<int> findAll2(List<int> items, int find) {
  var found = List<int>();
  items.asMap().forEach((idx,value){
    if(value == find)
      found.add(idx);
  });
  return found;
}

dartpad.dev/