Checking if an integer permutation is cyclic in Java
Here


int nextNumber = array[array[0]];
int visitedNumbers = 1;

while (nextNumber != array[0]) {
    nextNumber = array[nextNumber];
    visitedNumbers++;
}



you determine the length of the orbit starting array[0]. If you start at 0 instead then some array lookups are saved, and I find it easier to understand:

int nextNumber = array[0];
int visitedNumbers = 1;

while (nextNumber != 0) {
    nextNumber = array[nextNumber];
    visitedNumbers++;
}


And with a do-while loop the initial array lookup can be made part of the loop:

int currentNumber = 0;
int visitedNumbers = 0;

do {
    currentNumber = array[currentNumber];
    visitedNumbers++;
} while (currentNumber != 0);




Further suggestions:

Checking for duplicate array elements is not necessary if you limit the number of iterations:

int currentNumber = 0;
int visitedNumbers = 0;

do {
    currentNumber = array[currentNumber];
    visitedNumbers++;
} while (visitedNumbers &lt;= array.length &amp;&amp; currentNumber != 0);


This saves one array traversal and the additional storage in checkArrayElements(). The only downside is that calling the function with an invalid permutation now possibly returns false instead of throwing an error.

Similarly, the “bounds check” can be made directly in that loop. That might be viewed as a violation of the “separation of concerns” principle, but saves one array traversal.