Partition Array by Parity of Frequency
Arrays & HashingAuto-graded · 17 test casesLast updated
Tests in-place partitioning using a hash map to precompute element frequencies, then a two-pointer scheme to rearrange elements without extra array allocation.
Partition Array by Parity of Frequency
You are given a list of integers nums. Your task is to rearrange it in-place so that all elements whose value appears an odd number of times in the original list come before all elements whose value appears an even number of times.
Within each group the relative order of elements must be preserved (i.e. this is a stable partition).
Return the rearranged list (the same list object, modified in-place, is also acceptable).
Tie-breaking / uniqueness: Because the partition is stable, exactly one output is correct for every input.
Details
- Count frequencies using the original list before any rearrangement.
- An element belongs to the odd-frequency group if the count of that element in the original list is odd, and to the even-frequency group otherwise.
- Elements that never appear (count 0) are impossible by definition; every element in the list has count ≥ 1.
- The function must return the rearranged list.
Constraints
1 ≤ len(nums) ≤ 2000-10^6 ≤ nums[i] ≤ 10^6- The list may contain duplicates.
- An empty list
[]should be returned as-is.
Examples
Example 1
Input: partition_by_frequency_parity([3, 1, 2, 1, 3, 2, 2])
Output: [2, 2, 2, 3, 1, 1, 3]
Example 2
Input: partition_by_frequency_parity([5, 5, 7, 7, 7, 1])
Output: [7, 7, 7, 1, 5, 5]
Example 3
Input: partition_by_frequency_parity([4])
Output: [4]
Hints
Hint 1
First, build a frequency map of the original list in one pass before touching element positions.
Hint 2
A stable partition can be achieved without an auxiliary array by collecting the two groups separately, then writing them back — but think about whether you can also do it with two pointers plus a temporary buffer of O(1) extra space per swap step.
Hint 3
After filling in both groups in order, concatenate (odd-frequency group) + (even-frequency group) and copy the values back into the original list, then return it.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
The problem has two independent sub-problems:
- Classify each element as odd-frequency or even-frequency based on the original counts.
- Stably partition the array so the odd-frequency group leads.
Because stability is required (relative order preserved), a classic two-pointer swap in-place would disturb the order of elements. The cleanest correct approach is therefore:
- Build a frequency map (
Counter) in O(n). - Single pass over the original list, routing each element into one of two temporary lists (
odd_group,even_group) — this preserves relative order within each group automatically. - Concatenate and write back into
numsin-place, then return it.
Why It Is Correct
- Frequencies are computed before any mutation, so every classification uses the original counts.
- Elements are visited left-to-right once, so both groups retain their original relative ordering — satisfying the stability requirement.
- Writing back in-place satisfies the "in-place" spirit while keeping the logic simple and bug-free.
Complexity
| Value | |
|---|---|
| Time | O(n) — one pass for counting, one for partitioning, one for writing back |
| Space | O(n) — the two temporary lists together hold exactly n elements |
The space could theoretically be reduced to O(k) where k is the number of distinct values (for the Counter only) if we accepted an O(n²) stable-insertion approach, but O(n) extra space is the standard trade-off for a linear-time stable partition.
Reference Solution
from collections import Counter
def partition_by_frequency_parity(nums: list) -> list:
if not nums:
return nums
# Step 1: count frequencies in the ORIGINAL list
freq = Counter(nums)
# Step 2: stable partition — gather odd-freq elements then even-freq elements
odd_group = []
even_group = []
for x in nums:
if freq[x] % 2 == 1:
odd_group.append(x)
else:
even_group.append(x)
# Step 3: write back in-place
merged = odd_group + even_group
for i, val in enumerate(merged):
nums[i] = val
return nums
Worked Example
Input: [3, 1, 2, 1, 3, 2, 2]
Frequencies: {3: 2, 1: 2, 2: 3}
3→ even (count 2)1→ even (count 2)2→ odd (count 3)
Odd group (values with odd count, in original order): [2, 2, 2]
Even group (values with even count, in original order): [3, 1, 1, 3]
Result: [2, 2, 2, 3, 1, 1, 3]