Answer:
import java.util.ArrayList;
import java.util.Collections;
public class MyArrayList<T> extends ArrayList<T> {
public boolean add(T obj) {
if (Collections.frequency(this, obj) < 2) {
return super.add(obj);
}
return false;
}
}
Explanation:
According to the requirement, an element with the same value can occur at most twice in the list.
The Collections.frequency utility is useful to count the occurrances.