954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to find whether certain elements in array are equal to one another?

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 :)

gingerfish
Light Poster
47 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

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.

Thijk
Newbie Poster
16 posts since May 2011
Reputation Points: 10
Solved Threads: 3
 
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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: