for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if (array[i] == array[j]) return true;
}
}
Edit: Forgot to add the outer loop. i is initialized to zero.
Why is this code executing (n/2) times instead of (n) times?
This loop executes n/2 times on the average:
If you add the first line to the last, the second to the second from the back, the third to the third from the back and so on, each pair would yield n-1; there would be n/2 such pairs for even values of n, so an average number of times the loop executes over n is n/2.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.