# Intersection() method

**URL:** <https://forum.codewithmosh.com/t/intersection-method/16700>\
**Category:** Data Structures\
**Created:** [November 27, 2022, 9:46am UTC](https://forum.codewithmosh.com/t/intersection-method/16700 "2022-11-27T09:46:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raimon](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/raimon/32/5047_2.png) [@Raimon](https://forum.codewithmosh.com/u/Raimon)\
**Post date:** [November 27, 2022, 9:46am UTC](https://forum.codewithmosh.com/t/intersection-method/16700/1 "2022-11-27T09:46:04Z")

</div>

I have the same question as Jack007 at page:

> [@Array.intersect solution](https://forum.codewithmosh.com/t/array-intersect-solution/4470):
>
> public Array intersect(Array other) { var intersection = new Array(count); for (int item : items) if (other.indexOf(item) \>= 0) intersection.insert(item); return intersection; } Can someone please explain the second line? (Or the logic of the entire method itself cuz I don’t get it) The method is supposed to return elements common between two Arrays, however I don’t understand why that line gives the intended result. Doesn’t that line just check to see if the index is greater than or …

There was a solution given but I don’t understand. Can someone explain it to me?

This was part of the exercizes in the Arrays module lesson 10:

[https://codewithmosh.com/courses/639884/lectures/11425328](https://codewithmosh.com/courses/639884/lectures/11425328)

Lesson-question:

Extend the Array class and add a method to return the common items in this array and another array.

My question:

How do I use the intersection() method in the Array.

I have attached two screenshots to show my code.

Best,

Raimon

 ![Scherm­afbeelding 2022-11-27 om 10.13.17](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/c/c96ff3fb7ca0ab3de8f700cee7b199326e675008.png)  
 ![Scherm­afbeelding 2022-11-27 om 10.14.00](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/2/296a139626a29806afdfadfd96fdd6f81e3041ca.png)

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [November 27, 2022, 3:41pm UTC](https://forum.codewithmosh.com/t/intersection-method/16700/2 "2022-11-27T15:41:01Z")

</div>

You need to save the Array returned by the intersect method into a variable and then print that. Also, the intersect operation is really only useful with two different arrays.

For example:

```java
// ... setup numbers with 1..7
Array otherNumbers = new Array(3);
otherNumbers.insert(1);
otherNumbers.insert(5);
otherNumbers.insert(10);
Array intersection = numbers.intersect(otherNumbers);
intersection.print(); // should have 1 and 5

```

---

<div class="post-metadata">

**Author:** ![Raimon](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/raimon/32/5047_2.png) [@Raimon](https://forum.codewithmosh.com/u/Raimon)\
**Post date:** [November 27, 2022, 4:23pm UTC](https://forum.codewithmosh.com/t/intersection-method/16700/3 "2022-11-27T16:23:54Z")

</div>

Thank a lot jmrunkle, I’m going to try your explanation later. Just wanted to respond to show my appreciation.

---

<div class="post-metadata">

**Author:** ![Raimon](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/raimon/32/5047_2.png) [@Raimon](https://forum.codewithmosh.com/u/Raimon)\
**Post date:** [November 28, 2022, 9:55am UTC](https://forum.codewithmosh.com/t/intersection-method/16700/4 "2022-11-28T09:55:10Z")

</div>

Thanks again Jason, it worked 👍
