You can iterate over the items through items, shapes or connections. After getting a list, you can then look at the list of items and take it from there.
will bring you to the documentation on iteration of diagram items. Below is an example of how you would do it:
//your custom logic goes here