search method
Searches for an element in the stack and returns its 1-based position from the top.
Returns -1 if the element is not found. The top element is at position 1, the second element is at position 2, and so on.
Example:
final stack = Stack<String>();
stack.push('bottom');
stack.push('middle');
stack.push('top');
print(stack.search('top')); // 1
print(stack.search('middle')); // 2
print(stack.search('bottom')); // 3
print(stack.search('missing')); // -1
Implementation
int search(Object? element) {
for (int i = _elements.length - 1; i >= 0; i--) {
if (_elements[i] == element) {
return _elements.length - i;
}
}
return -1;
}