Q:

Create a function that checks to see if any indexes in the array are of the same value

I'm trying to create a function which checks to see if any two indexes in the array have the same value. I've realised my code below only checks to see if the first index is the same as the second, not checking if the first index is the same as the third, fourth and so on.
I tried using a for loop inside a for loop to compare each index, but I couldn't figure out how to get it to work.
Due to my current implementation, I also get an index out of bounds exception due to the i+1 exceeding the length of the array.
If anyone could help solve the code and explain to me how it works that would be great! Thanks!
public class Values {

public static void main (String[]args){

    int[] A = {0,1,2,1,4};

    for(int i = 0;i<=A.length;i++){

        int n = i+1;
        if(A[i] == A[n]){

        System.out.println("Index " + i + " is the same value as index " + n);
        System.out.println("Therefore, not all of the values in the array are different");
            break;
        }

    }
    System.out.println("All indexes in the array contain different values");
}

}

A:

The most efficient way is to sort first, then find if any two adjacent elements are equal:
Arrays.sort(A);
for (int i = 0; i < A.length - 1; i++) 
    if (A[i] == A[i+1])
        // do something

This algorithm has O(n log n) time complexity - due to the sort.
Using nested loops has O(n2) time complexity, which will start hurting with even modest array sizes. Plus the code is simpler.

