I want to check if a Rectangle(A Player) , intersects with one of the rectangles in a list (List).

I am currently using a for loop , which makes it slow , and poor performance.

```
for (int i = 0; i < gameObjects.objectList.Count; i++)
{
if (gameObjects.objectList[i].IntersectsWith(gameObjects.player))
{
gameObjects.objectList.RemoveAt(i); // if the player collided with an object, remove that object
}
}
```

How can I make it more efficient / is there another way to do it faster?

You can try organize your rectangles in a structure called

k-d-tree.It gives you up to O(log N) complexity in a large rectangle array (> 100).

F.e. make binary tree with fixed length, say, 2. Divide your space into left and right halves, then each half divide into top and bottom quarters (and so on).

Inside leaf node create a list on rectangles. If a rectangles falls into left half and top quarter, locate it in the list of this quarter.

A rectangle may be locates in a few list at the same time (f.e. in case if it falls in left and right halves).

To check intersection you should test rectangle in responding halves and quarters.

Or, you removes too many rectangles, it's faster to copy remaining rectangles into the new list in your own code.

Small example.

These all lines makes simple 2D-tree. First, make the tree:

Then, sort all rectangles in this tree:

Now you can fast get intersecting rectangles: