```python
def first(arr, x, n):
    """
    Finds the first position of an element in a sorted array.

    Args:
        arr: The sorted input array.
        x: The element to search for.
        n: The size of the array.

    Returns:
        The index of the first occurrence of x in arr, or -1 if not found.
    """
    low = 0
    high = n - 1
    result = -1

    while low <= high:
        mid = (low + high) // 2
        if arr[mid] > x:
            high = mid - 1
        elif arr[mid] < x:
            low = mid + 1
        else:  # arr[mid] == x
            result = mid
            high = mid - 1  # Try to find an earlier occurrence on the left
            
    return result
```