Rearrange Barcodes

Given barcodes represented as integers, rearrange them so that no two adjacent barcodes are equal. Frequency-driven placement or bucket assignment is the standard strategy for this bounded-label problem.

Input Format

barcodes = array of integers

Output Format

rearranged array with no equal adjacent values

Constraints

  • 1 <= barcodes.length <= 10^4; 1 <= barcodes[i] <= 10^4; it is guaranteed that a valid rearrangement exists

Examples

Example 1:

Input:

barcodes = [1,1,1,2,2,2]

Output:

[1,2,1,2,1,2]

Explanation:

High-frequency values are interleaved to avoid adjacent duplicates.

Example 2:

Input:

barcodes = [1,1,1,1,2,2,3,3]

Output:

[1,2,1,3,1,2,1,3]

Explanation:

The most frequent value is spaced out across the array.

Loading...
Rearrange Barcodes