Lonely Integer

  • + 0 comments

    JAVA 8

    public static int lonelyinteger(List<Integer> a) {
            List<Integer> newList = new ArrayList<Integer>();
    
            if (a.size() <= 1) {
                return a.get(0);
            } else {
                Collections.sort(a);
    
                for (Integer item : a) {
                    if (!newList.contains(item)) {
                        newList.add(item);
                    } else {
                        newList.remove(item);
                    }
                }
                return newList.get(0);
            }
        }