#3046
Easy Algorithms Split the array
Array Hash Table Counting
60.9% acceptance
Feb 25, 2026
168
16
You are given an integer array nums of even length. You have to split the array into two parts nums1 and nums2 such that:
nums1.length == nums2.length == nums.length / 2.
nums1 should contain distinct elements.
nums2 should also contain distinct elements.
Return true if it is possible to split the array, and false otherwise.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn is_possible_to_split(nums: Vec<i32>) -> bool {
let mut freq = std::collections::HashMap::new();
for &x in &nums { *freq.entry(x).or_insert(0) += 1; }
freq.values().all(|&v| v <= 2)
}
}