🌟 Lit AF Solution | O(n) Complexity | Beating 91.27% 🚀
IntuitionAlright fam, let's dive into this 🔥 problem! The goal here is to find the distinct difference for each element in an array. We gotta keep track of thos
### Intuition
Alright fam, let's dive into this 🔥 problem! The goal here is to find the distinct difference for each element in an array. We gotta keep track of those unique elements from both the left and the right sides of the array. Easy peasy! 😎

### Approach
1. **Calculate Unique Counts from Left**:
   - Use a `HashSet` to keep track of unique elements from the start of the array up to each index.
   - Store the size of the `HashSet` (which represents the number of unique elements) in the `ans` array.

2. **Calculate Unique Counts from Right and Compute Difference**:
   - Clear the `HashSet` and iterate from the end of the array to the beginning.
   - For each index, compute the difference between the number of unique elements to the left (already stored in `ans`) and the number of unique elements to the right (size of the `HashSet` during the second pass).

### Complexity
- **Time complexity**:
  - The time complexity is $$O(n)$$ because we traverse the array twice (once for left counts and once for right counts). 🔄

- **Space complexity**:
  - The space complexity is $$O(n)$$ due to the auxiliary array `ans` used to store the unique counts and the `HashSet`. 🧠

```java []
import java.util.HashSet;

class Solution {
    public int[] distinctDifferenceArray(int[] nums) {
        int[] ans = new int[nums.length];
        HashSet<Integer> set = new HashSet<>();
        
        // Calculate unique counts from the left
        for (int i = 0; i < nums.length; i++) {
            set.add(nums[i]);
            ans[i] = set.size();
        }
        
        set.clear();
        
        // Calculate unique counts from the right and compute difference
        for (int i = nums.length - 1; i >= 0; i--) {
            ans[i] -= set.size();
            set.add(nums[i]);
        }
        
        return ans;
    }
}
```

```python []
class Solution:
    def distinctDifferenceArray(self, nums):
        ans = [0] * len(nums)
        unique_left = set()
        
        # Calculate unique counts from the left
        for i in range(len(nums)):
            unique_left.add(nums[i])
            ans[i] = len(unique_left)
        
        unique_right = set()
        
        # Calculate unique counts from the right and compute difference
        for i in range(len(nums)-1, -1, -1):
            ans[i] -= len(unique_right)
            unique_right.add(nums[i])
        
        return 
```
```C++ []
#include <vector>
#include <unordered_set>

class Solution {
public:
    std::vector<int> distinctDifferenceArray(std::vector<int>& nums) {
        std::vector<int> ans(nums.size());
        std::unordered_set<int> set;
        
        // Calculate unique counts from the left
        for (int i = 0; i < nums.size(); i++) {
            set.insert(nums[i]);
            ans[i] = set.size();
        }
        
        set.clear();
        
        // Calculate unique counts from the right and compute difference
        for (int i = nums.size() - 1; i >= 0; i--) {
            ans[i] -= set.size();
            set.insert(nums[i]);
        }
        
        return ans;
    }
};

```

There you go! You've got solutions of Python, Java, and C++ to tackle this problem like a boss. 🚀✨ Let me know if you need more help or have any other questions! 😄👋
['Array', 'Hash Table', 'C++', 'Java', 'Python3']