voidmain(){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,intfind){varfound=List<int>();for(varidx=0;idx<items.length;idx++){if(items[idx]==find){found.add(idx);}}returnfound;}// alternative approach, which cuts it down maybe a line? lolList<int>findAll2(List<int>items,intfind){varfound=List<int>();items.asMap().forEach((idx,value){if(value==find)found.add(idx);});returnfound;}
Ebrahim Hasan is a full stack web developer, currently working as a freelancer on freelancer.com, he is a co-owner of a swedish and an Egyptian company.
JavaScript
Haskell
Written on the phone, it might not work, I'll check it when I get home.
In Python:
The commented out array line would allocate a new array to meet the specification.
Elm
Implementation.
In Dart:
dartpad.dev/
Haskell:
or alternatively,
Swift:
Rust:
In JavaScript:
function find_all1(arr, n){
let arr1 = [];
arr.forEach((item,index) => {
item === n ? arr1.push(index): null;
});
console.log(arr1);
}
find_all1([6,9,3,4,3,82,11],3);
Ruby
js
PHP
<?php function find_all($array,$number){
return array_keys($array,$number);
}
var_dump(find_all(array(6, 9, 3, 4, 3, 82, 11), 3) );