Search code examples
clinear-search

Linear search arrays


How do I print multiple searches from a linear search in C?

for (i=0; i < index; i++)
   if (array[i] == target)
          return i;

Is it possible to return more then one value, say, if the array has multiple elements that equals the target?


Solution

  • Instead of returning matching elements, you could print out their index values (allowing multiple values to be printed) or insert them into an array and then return that.