Skip to content

Linear search

Java loop + return index

Same task in: JavaScriptPHP

Flowchart (ISO 5807)

YesNoStartInput a, keyi = 0, |a| − 1, 1a[i] = keyReturn iEndReturn −1EndFigure 1 — Search.indexOf

Source code

Java
class Search {
    static int indexOf(int[] a, int key) {
        for (int i = 0; i < a.length; i++) {
            if (a[i] == key) {
                return i;
            }
        }
        return -1;
    }
}

The editor opens with this code — change it and the chart redraws.

Language
Java
Constructs
loop + return index
Topic
Algorithms
Formats
Word, Visio, draw.io, PDF, SVG

Related examples