Can you implement a solution to a specific algorithmic problem while optimizing for memory constraints?
Implement find_duplicate(nums). The input contains n + 1 integers, each in the range 1 through n, with exactly one value duplicated. Return the duplicated value without modifying nums and using O(1) extra space.
Input: a list of integers. Output: the duplicated integer.
def find_duplicate(nums):