private List < Integer > getEqualToUsedOnlyInEquivalentElements ( List < Integer > equalTo , List < Integer > equivalentElements ) { List < Integer > result = new ArrayList <> ( equalTo ) ; for ( int i = 0 ; i < equalElements . size ( ) ; i ++ ) { if ( ! alreadyMatched . get ( i ) ) { if ( ! equivalentElements . contains ( i ) ) { result . removeAll ( equalElements . get ( i ) ) ; } } } return result ; }
If there are more equivalent elements we can match those that are not used anywhere else we iterate over actual elements that are not in equivalentElements and from equalTo remove those that are used outside equivalent elements