public boolean containsDuplicate(int[] nums) {
        HashSet<Integer> hashSet = new HashSet<Integer>();
        int begin = 0, end = nums.length - 1;
        while (begin < end) {
            if (hashSet.contains(nums[begin]))
                return true;
            hashSet.add(nums[begin++]);
            if (hashSet.contains(nums[end]))
                return true;
            hashSet.add(nums[end--]);
        }
        return false;
    }