Chris Headleand Chris Headleand - 1 year ago 95
Java Question

Is it possible to find out if a value exists twice in an arraylist?

I have an integer arraylist..

ArrayList <Integer> portList = new ArrayList();

I need to check if a specific integer has already been entered twice. Is this possible in Java?

Answer Source

I know it's an old question, but since I was here looking for the answer, I thought I'd share MY solution

public static boolean moreThanOnce(ArrayList<Integer> list, int searched) {

    int numCount = 0;
    boolean more = false;

    for (int thisNum : list) {
        if (thisNum == searched) {
            numCount ++ ;

    if (numCount > 1) {
        more = true;

    return more;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download