Java solution for beginners using ArrayList


  • 0
    Y
    public int singleNumber(int[] nums) {
        int sum = 0;
        ArrayList<Integer> visited = new ArrayList<Integer>();
        for (int i = 0; i < nums.length; i++) {
            if (visited.contains(nums[i])) {
                sum -= nums[i];
            } else {
                visited.add(nums[i]);
                sum += nums[i];
            }
        }
        return sum;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.