Finding duplicate values between two arrays

前端 未结 6 1193
死守一世寂寞
死守一世寂寞 2020-12-09 22:56

Let\'s say I have the following two arrays:

int[] a = [1,2,3,4,5];
int[] b = [8,1,3,9,4];

I would like to take the first value of array

6条回答
  •  生来不讨喜
    2020-12-09 23:19

    Since you haven't got this tagged as homework, I'll give you the benefit of the doubt. As you said you'll need two loops; loop foreach int in a[] and foreach int in b[]. Then just compare the two values at each iteration, which gives you the simple code of:

    for (int x : a) {
       for (int y : b) {
          if (x == y) {
             System.out.println("a[] and b[] both contain " + x);
          }
       }
    }
    

提交回复
热议问题