comparing elements of the same array in java
Asked Answered
N

5

8

I am trying to compare elements of the same array. That means that i want to compare the 0 element with every other element, the 1 element with every other element and so on. The problem is that it is not working as intended. . What i do is I have two for loops that go from 0 to array.length-1.. Then i have an if statement that goes as follows: if(a[i]!=a[j+1])

for (int i = 0; i < a.length - 1; i++) {
    for (int k = 0; k < a.length - 1; k++) {
        if (a[i] != a[k + 1]) {
            System.out.println(a[i] + " not the same with  " + a[k + 1] + "\n");
        }
    }
}
Noella answered 4/5, 2014 at 19:6 Comment(0)
P
54

First things first, you need to loop to < a.length rather than a.length - 1. As this is strictly less than you need to include the upper bound.

So, to check all pairs of elements you can do:

for (int i = 0; i < a.length; i++) {
    for (int k = 0; k < a.length; k++) {
        if (a[i] != a[k]) {
            //do stuff
        }
    }
}

But this will compare, for example a[2] to a[3] and then a[3] to a[2]. Given that you are checking != this seems wasteful.

A better approach would be to compare each element i to the rest of the array:

for (int i = 0; i < a.length; i++) {
    for (int k = i + 1; k < a.length; k++) {
        if (a[i] != a[k]) {
            //do stuff
        }
    }
}

So if you have the indices [1...5] the comparison would go

  1. 1 -> 2
  2. 1 -> 3
  3. 1 -> 4
  4. 1 -> 5
  5. 2 -> 3
  6. 2 -> 4
  7. 2 -> 5
  8. 3 -> 4
  9. 3 -> 5
  10. 4 -> 5

So you see pairs aren't repeated. Think of a circle of people all needing to shake hands with each other.

Peccadillo answered 4/5, 2014 at 19:12 Comment(7)
"That means that i want to compare the 0 element with every other element, the 1 element with every other element and so on." The better approach might not be useful if he really wants ^Hast
@Hast my first example does exactly that. I elaborate that given the comparison the OP is doing this might not be the best approach.Peccadillo
You don't need the if (a[i] != a[k]) { check.Blum
@MarkKnol that's the whole point of the exercise - of course you need it. The OP is checking each pair of elements for non equal elements. Why? Only the OP knows that.Peccadillo
But if k starts at i+1, then there is never a equal element, or am I missing something? I get the same results without the condition.Blum
@MarkKnol it's not i != k it's a[i] != a[k] - i.e that the array values are not equal, not the indexes.Peccadillo
Ah thats the difference ofcourse. I assumed the array was unique already.Blum
H
1

Try this or purpose will solve with lesser no of steps

for (int i = 0; i < a.length; i++) 
{
    for (int k = i+1; k < a.length; k++) 
    {
        if (a[i] != a[k]) 
         {
            System.out.println(a[i]+"not the same with"+a[k]+"\n");
        }
    }
}
Hydrolysis answered 4/5, 2014 at 19:25 Comment(1)
you're not taking the last element into consideration at all...it should be "a.length" in inner loopHast
H
0
for (int i = 0; i < a.length; i++) {
    for (int k = 0; k < a.length; k++) {
        if (a[i] != a[k]) {
            System.out.println(a[i] + " not the same with  " + a[k + 1] + "\n");
        }
    }
}

You can start from k=1 & keep "a.length-1" in outer for loop, in order to reduce two comparisions,but that doesnt make any significant difference.

Hast answered 4/5, 2014 at 19:22 Comment(0)
B
0

How can I compare the value of a[i] with a [a+1] and also with a[a-1] and also the value of the botton index ( related k)? to find out if the value of current index is less than all other its neighbor

for (int i = 0; i < a.length; i++) {
    for (int k = 0; k < a.length; k++) {
        if (a[i] < a[i+1]) && (a[i] < a[i-1]) )  {
            int b = a[i]
        }
    }
}    
   for (int i = 0; i < a.length; i++) {
    for (int k = 0; k < a.length; k++) {
        if (a[k] < a[k+1]) && (a[k] < a[k-1]) )  {
            int c = a[k]
        }
    }
}      
Bratcher answered 5/6, 2022 at 4:23 Comment(0)
S
-2

Try below code. This logic will help to compare elements of an array.

    int[] arr= {1,7,22,55,22,6,6,7};
    
    for (int i = 0; i < arr.length; i++) {
        for (int j = i+1; j < arr.length; j++) {
            if(arr[i]==arr[j])
            {                   
                System.out.print(arr[i] + " ");
            }
        }
    }
Solley answered 25/4, 2021 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.