DEV Community
•
2026-03-24 15:35
Sort an Array of 0s, 1s and 2s
Sort an Array of 0s, 1s and 2s
Problem Statement
Given an array consisting of only 0s, 1s and 2s, sort the array in ascending order.
Examples
Input
arr = [0, 2, 1, 2, 0]
Output
[0, 0, 1, 2, 2]
Input
arr = [2, 0, 1]
Output
[0, 1, 2]
Approach 1 Using Sorting
The simplest way is to sort the array using built in functions.
Code
...