Given a matrix of ArrayList<Integer>, I often write in my code
int val = 2;
for(int i = 0 ; i < N ;i++){
if(matrix[i][0].list.contains(val))
matrix[i][0].remove(val);
}
Since both .contains() and .remove() run in O(n) time, it got me wondering if there exists a way of combining these two statements and perhaps, thereby increasing run time efficiency somehow? I have done sufficient research on this over a few days span, I was not able to find a convincing answer.