```python
def search(arr, n):
    """
    Finds the element that appears only once in a sorted array where all other
    elements appear twice.
    This function uses a binary search approach for an O(log n) time complexity.
    """
    low = 0
    high = n - 1

    # The loop continues until low and high converge to the index of the single element.
    # The search space is halved in each iteration.
    while low < high:
        mid = low + (high - low) // 2
        
        # We want to ensure we are looking at the start of a pair.
        # The first element of any pair is at an even index.
        # If mid is odd, we move it to the previous even index.
        if mid % 2 == 1:
            mid -= 1
            
        # Now, mid is an even index. We check the pair (arr[mid], arr[mid+1]).
        # If the elements in the pair are the same, it means the single element
        # is not in the left part of the array (from index 0 to mid+1),
        # because all elements up to this point form complete pairs.
        # So, we can safely discard the left half and move our search to the right.
        if arr[mid] == arr[mid + 1]:
            low = mid + 2
        # If the elements in the pair are different, it means the single element
        # has broken the pattern of pairs. The single element must be in the
        # left half of the array (including the element at mid).
        # So, we shrink our search space to the left half.
        else:
            high = mid
            
    # When the loop terminates, low and high will be at the same index,
    # which is the index of the single element.
    return arr[low]

```