🏆 Easy to understand |⚡ Optimal O(N + M) | 🚀 Clean & Efficient
🔹 Intuition & Approach 🔹💡 Think of it as a "difference filter"!
We need to find numbers that are unique in nums1 and nums2, meaning they appear in one but not i
### **🔹 Intuition & Approach 🔹**  

💡 **Think of it as a "difference filter"!**  
We need to find numbers that are **unique** in `nums1` and `nums2`, meaning they appear in one but **not** in the other.  

---

### **🚀 Steps to Solve:**  
1️⃣ **Store all unique numbers in sets** → 📦 `set1` for `nums1`, 📦 `set2` for `nums2`.  
2️⃣ **Find elements in `set1` but not in `set2`** → 🧐 If `set2` **doesn’t contain** a number from `set1`, add it to the answer!  
3️⃣ **Find elements in `set2` but not in `set1`** → Same process as step 2.  
4️⃣ **Return both lists as a single answer!** ✅  

---

### **📌 Example for Better Understanding:**  
#### **Input:**  
```java
nums1 = [1, 2, 3, 3]  
nums2 = [2, 4, 6]
```
#### **Steps:**  
- `set1` → `{1, 2, 3}` ✅ (removes duplicate `3`)  
- `set2` → `{2, 4, 6}` ✅  

- Elements **in `set1` but not in `set2`** → `{1, 3}` ➝ `[1, 3]`  
- Elements **in `set2` but not in `set1`** → `{4, 6}` ➝ `[4, 6]`  

#### **Output:**  
```java
[[1, 3], [4, 6]]
```

---

### **⏱️ Time & Space Complexity:**
#### **⏳ Time Complexity:**
- **Adding elements to `HashSet`** → `O(N + M)`  
- **Checking for distinct elements** → `O(N + M)`  
- **Overall Complexity** → **O(N + M) ✅ (Most Optimal Solution)**  

#### **🗂️ Space Complexity:**
- **Two HashSets (`set1` & `set2`)** → `O(N + M)`  
- **Two result lists (`list1` & `list2`)** → `O(N + M)`  
- **Overall Complexity** → **O(N + M) ✅**  

---

### **🌟 Why Use `HashSet`?**
⚡ **Fast Lookup (`O(1)`)** → Checking if an element exists is super quick!  
🚀 **Removes duplicates automatically** → No extra work needed!  
🎯 **Optimized for large inputs** → No unnecessary loops!  

---

# Code
```java []
class Solution {
    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
        // Use HashSets to store unique elements from both arrays
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();

        // Populate set1 with elements from nums1
        for (int num : nums1) {
            set1.add(num);
        }

        // Populate set2 with elements from nums2
        for (int num : nums2) {
            set2.add(num);
        }

        // Lists to store elements that are unique to each array
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();

        // Find elements in set1 that are NOT in set2
        for (int num : set1) {
            if (!set2.contains(num)) {
                list1.add(num);
            }
        }

        // Find elements in set2 that are NOT in set1
        for (int num : set2) {
            if (!set1.contains(num)) {
                list2.add(num);
            }
        }

        // Return the two lists as a single result
        return Arrays.asList(list1, list2);
    }
}

```
``` python []
class Solution:
    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
        set1, set2 = set(nums1), set(nums2)

        list1 = [num for num in set1 if num not in set2]
        list2 = [num for num in set2 if num not in set1]

        return [list1, list2]

```
``` cpp []
class Solution {
public:
    vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> set1(nums1.begin(), nums1.end());
        unordered_set<int> set2(nums2.begin(), nums2.end());

        vector<int> list1, list2;

        for (int num : set1) {
            if (set2.find(num) == set2.end()) {
                list1.push_back(num);
            }
        }

        for (int num : set2) {
            if (set1.find(num) == set1.end()) {
                list2.push_back(num);
            }
        }

        return {list1, list2};
    }
};

```
![upvote.jpg](https://assets.leetcode.com/users/images/e71dfccb-bdfe-4602-bf42-387e13c53c0d_1741462372.0380337.jpeg)

['Array', 'Hash Table', 'Hash Function', 'Java']