Not an elegant way (, but it works)
Assuming that you have a "lines" layer with a numeric field named "id".
Assuming also that you have a "polygons" layer with a numeric field named "max_id", which represents the maximum id value of the linestrings that are within each polygon.
You can Select by Expression the linestring features that have an id value that matches with any max_id value in the polygons layer, using the lines layer as input and the following expression:
First, the expression builds an aggregate (by concatenation) of the values in the max_id field of the polygons layer. Returns something like:
Then, builds a new string with that one, which starts and ends with two commas. Returns something like:
In the other hand, also builds a string with the "id" attribute for each linestring feature, which starts with
%, and ends with
,%. Returns something like
'%,4,%' for the feature with the id value = 4.
Finally, it compares, if
',,4,25,76,,' is like
'%,4,%' (% symbols are wildcards), and returns
false. That determines if the feature must be selected or not.
DISCLAIMER: Tested with a few features. The performance for a lot of them is not guaranteed.