Finding duplicate values between two arrays

前端 未结 6 1197
死守一世寂寞
死守一世寂寞 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:15

    Yes, you need two loops, and yes, nested.

    pseudocode it will look like:

    for each in A do
        for each in B do
           if (current item of A equals to current item of B)
               say yes!
        done
    done
    

    Now everything you need is to translate it to Java. Since it sounds like a homework or some exercise you should do it by yourself.

    Also, think about what output you need. If you just need a true/false whether a and b have some common values, then you can exit the loops as soon as you find the first match. If instead you need to count the number of common elements between the arrays, you'll need to throw a counter into that set of nested loops. I'll leave it up to you to figure out that portion.

提交回复
热议问题