Description
Given an array of barcodes, rearrange them so that no two adjacent barcodes are equal. You can assume valid arrangement exists. Return the result as an array.
Examples
Input:
barcodes = [1,1,1,2,2,2]Output:
[1,2,1,2,1,2]Explanation:
Alternating arrangement.
Input:
barcodes = [1]Output:
[1]Explanation:
Edge case with a single-element array.
Input:
barcodes = [1,1,1,1,2,2,3]Output:
[1,2,1,2,1,3,1]Explanation:
When one barcode appears much more frequently than others, placing the most frequent barcode (1) in every other position first, then fill remaining positions with less frequent barcodes (2,3) to ensure no two adjacent barcodes are the same.
Constraints
- •
1 ≤ barcodes.length ≤ 10000