【Video】Simple Hash solution - Python, JavaScript, Java and C++
Intuition\nWe don\'t have to care about length of 2 arrays.\n\n# Solution Video\n\nhttps://youtu.be/YLZVibZ5RW8\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to
# Intuition\nWe don\'t have to care about length of 2 arrays.\n\n# Solution Video\n\nhttps://youtu.be/YLZVibZ5RW8\n\n### \u2B50\uFE0F\u2B50\uFE0F Don\'t forget to subscribe to my channel! \u2B50\uFE0F\u2B50\uFE0F\n\n**\u25A0 Subscribe URL**\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\nSubscribers: 3,916\nThank you for your support!\n\n---\n\n# Approach\n\nIn this problem, the task is to determine the conditions under which it\'s not possible to create two arrays satisfying the given conditions, rather than focusing on the lengths of the two arrays, as implied by the description\'s mention of an even length input array.\n\n```\nInput: nums = [1,1,2,2,3,4]\n```\nBased on the description, we can create \n```\nnums1 = [1,2,3]\nnums2 = [1,2,4]\n```\nLet\'s think about when frequency of numbers is `1`. In this case, we have one `3` and `4`. Since above example is true case, if freqency of numbers is `1`, we can create two arrays with the condition the description says.\n\nHow about frequency of numbers is `2`. We have two `1` and `2`. Seems like that is also fine.\n```\nInput: nums = [1,1,2,2,3,4,1,5]\n```\nI added new numbers, `1` and `5`.\n\nLet\'s think about this case. we can create `[1,2,3]` and `[1,2,4]`. Let\'s add `5` at first. frequency of `5` is `1`, so we can put `5` into `nums1` or `nums2`. Let\'s put `5` into `nums1`\n\n```\nnums1 = [1,2,3,5]\nnums2 = [1,2,4]\n```\nLet\'s try to put `1` into `nums2`\n```\nnums1 = [1,2,3,5]\nnums2 = [1,2,4,1]\n```\nWe break the condition the description says, because `1` in `nums2` is duplicate number.\n\nHow about swtiching `1` and `5`. It\'s the same result. `1` is duplicate number in `nums1`.\n\nFrom this example,\n\n---\n\n\u2B50\uFE0F Points\n\nIf we have 3 same numbers or more, we should return `false`.\n\n---\n\nEasy!\uD83D\uDE06\nLet\'s see solution codes and step by step algorithm!\n\n---\n\n\u2B50\uFE0F I recently created a video on how I\'ve been using LeetCode to learn.\n\nhttps://youtu.be/bU_dXCOWHls\n\n\n---\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n```python []\nclass Solution:\n    def isPossibleToSplit(self, nums: List[int]) -> bool:\n        count = {}\n\n        for n in nums:\n            count[n] = 1 + count.get(n, 0)\n        \n        for val in count.values():\n            if val > 2:\n                return False\n        \n        return True\n```\n```javascript []\n/**\n * @param {number[]} nums\n * @return {boolean}\n */\nvar isPossibleToSplit = function(nums) {\n    let count = new Map();\n\n    for (let n of nums) {\n        count.set(n, (count.get(n) || 0) + 1);\n    }\n    \n    for (let val of count.values()) {\n        if (val > 2) {\n            return false;\n        }\n    }\n    \n    return true;    \n};\n```\n```java []\nclass Solution {\n    public boolean isPossibleToSplit(int[] nums) {\n        HashMap<Integer, Integer> count = new HashMap<>();\n\n        for (int n : nums) {\n            count.put(n, count.getOrDefault(n, 0) + 1);\n        }\n        \n        for (int val : count.values()) {\n            if (val > 2) {\n                return false;\n            }\n        }\n        \n        return true;        \n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    bool isPossibleToSplit(vector<int>& nums) {\n        unordered_map<int, int> count;\n\n        for (int n : nums) {\n            count[n]++;\n        }\n        \n        for (auto& pair : count) {\n            if (pair.second > 2) {\n                return false;\n            }\n        }\n        \n        return true;        \n    }\n};\n```\n\n## Step by step algorithm\n\n1. **Initialize a dictionary to count occurrences of each number:**\n\n```python\ncount = {}\n```\n\nThis dictionary will store the count of occurrences of each number in the `nums` array.\n\n2. **Count occurrences of each number in the array:**\n\n```python\nfor n in nums:\n    count[n] = 1 + count.get(n, 0)\n```\n\nThis loop iterates through each number `n` in the `nums` array. It updates the count of `n` in the `count` dictionary by 1, adding 1 to the existing count if `n` is already a key in the dictionary, or initializing the count to 1 if `n` is encountered for the first time.\n\n3. **Check if any number appears more than twice:**\n\n```python\nfor val in count.values():\n    if val > 2:\n        return False\n```\n\nThis loop iterates through the values of the `count` dictionary, which represent the counts of each number in the `nums` array. If any count exceeds 2, it means that there are more than two occurrences of some number in the array. In such a case, the function returns `False`, indicating that it\'s not possible to split the array into two parts satisfying the conditions.\n\n4. **Return `True` if no number appears more than twice:**\n\n```python\nreturn True\n```\n\nIf the loop completes without finding any number appearing more than twice, it means that the `nums` array can be split into two parts where each part contains distinct elements and has equal lengths. Therefore, the function returns `True`.\n\nThis algorithm efficiently determines if it\'s possible to split the array according to the given conditions.\n\n---\n\nThank you for reading my post.\n\u2B50\uFE0F Please upvote it and don\'t forget to subscribe to my channel!\n\n\u25A0 Subscribe URL\nhttp://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1\n\n\u25A0 Twitter\nhttps://twitter.com/CodingNinjaAZ\n\n\u2B50\uFE0F My previous video\nhttps://youtu.be/0EHXLXz-Q5c\n
['C++', 'Java', 'Python3', 'JavaScript']