# Answer to Exercise 8

Like exercise 7, we split the numbers variable to create an array. The outer for loop grabs 1 number at a time from the array. The inner loop then checks the rest of the numbers to see if there is a match. But have a look at the initialisation of the inner loop:

int j = i + 1

The j variable is being set to whatever value i is (the outer loop) plus 1. This ensures that you're not checking the numbers from the start of the array all the time. You're checking only in a forwards direction. For example, suppose i has a value 0. This would grab the number 9 from our array. The inner loop would set j to i + 1, which is a value of 1. So the inner loop would check the numbers from 8 to the end of the array, which is 6. If a match is found then we can increment the counter. The next time round the outer loop, the number from the array would be 8. The inner loop would then set j to be i + 1, which is 2. Which means that the inner loop checks the numbers 7, 8 and 6 looking for a match.

### C# NET Answer

``` private void btnPairs_Click(object sender, EventArgs e)
{
string numbers = "9-8-7-8-6";
string[] array_numbers = numbers.Split('-');

int counter = 0;

for (int i = 0; i < array_numbers.Length - 1; i++)
{
string pair_check = array_numbers[i];

for (int j = i + 1; j  < array_numbers.Length; j ++)
{
if (pair_check == array_numbers[j])
{
counter++;
}
}
}

if (counter > 0)
{
MessageBox.Show("At least one pair found");
}
}
```

### VB NET Answer

```Private Sub btnPairs_Click(sender As Object, e As EventArgs) Handles btnPairs.Click

Dim numbers = "9-8-7-8-6"
Dim array_numbers() As String

array_numbers = numbers.Split("-")
Dim counter As Integer = 0

For i = 0 To array_numbers.Length - 1

Dim pair_check = array_numbers(i)

For j = i + 1 To array_numbers.Length - 1
If pair_check = array_numbers(j) Then
counter = counter + 1
End If
Next
Next

If counter > 0 Then
MessageBox.Show("At least one pair found")
End If

End Sub
```

### Java Answer

```public static void main(String[] args) {

String numbers = "9-8-5-7-8";
String[] array_numbers = numbers.split("-");

int counter = 0;

for (int i = 0; i < array_numbers.length - 1; i++)
{
String pair_check = array_numbers[i];

for (int j = i + 1; j  < array_numbers.length; j ++)
{
if (pair_check.equals(array_numbers[j]))
{
counter++;
}
}
}

if (counter > 0) {
System.out.println("At least one pair found");
}
}
```