In this code example all pairs of numbers in a list are recognized and output with indication of the position.
import java.util.Scanner;
class ArrayExample {
public static void getPairs(int[] myList) {
for (int i = 0; i < myList.length; i++) {
for (int j = i + 1; j < myList.length; j++) {
if (myList[i] == myList[j]) {
System.out.println("Number " + myList[i] + " is included two times, at indices " + i + " and " + j);
}
}
}
}
public static void main(String[] args) {
int[] myList = { 3, 5, 6, 7, 8, 4, 2, 5, 4, 2 };
getPairs(myList);
}
}
Number 5 is included two times, at indices 1 and 7
Number 4 is included two times, at indices 5 and 8
Number 2 is included two times, at indices 6 and 9