I read in many places that view functions do not cost gas however that's not true because they cost gas for blockchain operations even if those do not change the state. Correct me if I am wrong For example:
pragma solidity ^0.4.17;
contract myContract {
uint[] public anArray; /// assume it has 1,000,000 (1Million) elements
/* function which returns how many times a specific element is in the n array*/
function elementIndex(uint element) public view returns(uint){
uint counter;
for(uint i; i<anArray.length; i++){
if(anArray[i] == element){
counter++;
}
}
return counter;
}
function returnAllElements() public view returns(uint[]){
return anArray;
}}
The real problem comes on the front end when the array has 1 million elements. Running elementIndex() will never work because the iteration will take too long. returnAllElements will not work either (I assume).
What can be done in such circumstances?