O(n) - using HashSet
Complexity
Time complexity: O(n)

Space complexity: O(n)
Code
# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```rust []
use std::collections::HashSet;

impl Solution {
    pub fn distinct_difference_array(nums: Vec<i32>) -> Vec<i32> {
        // store the distinct element count
        let n: usize = nums.len();
        let mut ans: Vec<i32> = vec![0;n];
        let mut prefix: Vec<i32> = vec![0;nums.len()];
        let mut seen_prefix: HashSet<i32> = HashSet::new();
        let mut sufix: Vec<i32> = vec![0;nums.len()];
        let mut seen_sufix: HashSet<i32> = HashSet::new();


        // iterate backward and populate the prefix sum
        for i in 0..n {
            let rev_i = n - i - 1;
            sufix[rev_i] = seen_sufix.len() as i32;
            seen_sufix.insert(nums[rev_i]);

            seen_prefix.insert(nums[i]);
            prefix[i] = seen_prefix.len() as i32;
        }

        for i in 0..n {
            ans[i] = prefix[i] - sufix[i];
        }

        return ans;
    }
}
```
['Rust']