how to find out whether certain elements in an array are equal to one another?
for this array: int array[] = {0, 1, 0, 0, 2, 0, 0, 3, 0, 1}; i wanna know if elements under the indexes 0, 1, and 9 are same numbers

well the easy way is:

if(array[0] == array[1] && array[1]==array[9])
{
    System.out.println("These are same elements!");
}

or something like this, for testing only once

but the thing is, i want to test different set of elements in the array by looping.

any ideas? :?:
thank you :)

Recommended Answers

All 2 Replies

You can put it in a method with an array as the parameter.

static void checkArray(int[] array) 
{
   if(array[0] == array[1] && array[1]==array[9])
       System.out.println("These are same elements!");
}

Then you can check all your arrays in a loop.

i want to test different set of elements in the array by looping.

Do you know which set of elements you want to check?
In the example you wanted to look at 0,1 and 9.

Are there other sets of specific indexes to check?
Are there always exactly 3 indexes in the set?
Put the indexes in a 2 dim array with each row containing the 3 indexes to be checked.
Then use the contents of that array to check the other array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.