Sorting Array of Exactly Three Unique Repeating Elements in JavaScript
Last Updated :
15 May, 2024
Given an array of Numbers that contain any frequency of exactly three unique elements. Our task is to sort this array using JavaScript.
Examples:
Input: arr[] = [3, 1, 3, 2, 1, 2, 1, 3, 2]
Output: [1, 1, 1, 2, 2, 2,3,3,3]
Input = arr[] =[1,2,3,1,2,3]
Output: [1,1,2,2,3,3]
In this approach, we use counting sort to sort the array. We count the occurrences of each unique element and then construct the sorted array based on the counts.
Example: Implementation of a program to sort an array of exactly three unique repeating elements using Counting Sort with Extra Space
JavaScript
function sortArray(arr) {
const counts = {};
arr.forEach(num => {
counts[num] = (counts[num] || 0) + 1;
});
const sortedArr = [];
for (let i = 0; i < 3; i++) {
const num = i + 1;
if (counts[num]) {
sortedArr.push(...Array(counts[num]).fill(num));
}
}
return sortedArr;
}
const arr = [3, 1, 3, 2, 1, 2, 1, 3, 2];
const sortedArr = sortArray(arr);
console.log("Sorted array:", sortedArr);
OutputSorted array: [
1, 1, 1, 2, 2,
2, 3, 3, 3
]
Time Complexity: O(n)
Auxiliary Space: O(n)
Three-Way Partitioning
In this approach we use three-way partitioning technique to sort the array in place. Here we maintain three pointers (low, mid, and high) to partition the array into three sections and thus sortiing them.
Example: Implementation of program to sort array of exactly three unique repeating elements using Three-Way Partitioning
JavaScript
function sortArray(arr) {
let l = 0, m = 0, h = arr.length - 1;
while (m <= h) {
if (arr[m] === 1) {
[arr[l], arr[m]] = [arr[m], arr[l]];
l++;
m++;
} else if (arr[m] === 2) {
m++;
} else {
[arr[m], arr[h]] = [arr[h], arr[m]];
h--;
}
}
return arr;
}
const arr = [3, 1, 3, 2, 1, 2, 1, 3, 2];
const sortedArr = sortArray(arr);
console.log("Sorted array:", sortedArr);
OutputSorted array: [
1, 1, 1, 2, 2,
2, 3, 3, 3
]
Time Complexity: O(N),where n is the length of the input array
Auxiliary Space: O(1)
Similar Reads
JavaScript Program to Find the Most Frequently Occurring Element in an Array In this article, we are going to learn how to find the most frequently occurring element in an array we can use objects to count the occurrences of each element in an array. Methods to find the most frequently occurring element in an array in JavaScript: Table of Content Method 1: Using the reduce()
3 min read
JavaScript Program to Count Unequal Element Pairs from the Given Array Unequal element pairs in an array refer to pairs of distinct elements within the array that have different values. These pairs consist of two elements that are not equal to each other, highlighting their inequality when compared in the context of the array's values.Examples:Input: arr[] = {6, 5, 2,
4 min read
JavaScript Program to Find the First Non-Repeated Element in an Array Finding the first non-repeated element in an array refers to identifying the initial occurrence of an element that does not occur again elsewhere within the array, indicating uniqueness among the elements.Examples: Input: {-1, 2, -1, 3, 0}Output: 2Explanation: The first number that does not repeat i
5 min read
JavaScript Program to Check if Kth Index Elements are Unique In this article, We have given a String list and a Kth index in which we have to check that each item in a list at that particular index should be unique. If they all are unique then we will print true in the console else we will print false in the console.Example:Input: test_list = [âgfgâ, âbestâ,
6 min read
JavaScript Program for the Minimum Index of a Repeating Element in an Array Given an array of integers arr[], The task is to find the index of the first repeating element in it i.e. the element that occurs more than once and whose index of the first occurrence is the smallest. In this article, we will find the minimum index of a repeating element in an array in JavaScript.E
5 min read
JavaScript Program to Find Kâth Non-Repeating Character in String The K'th non-repeating character in a string is found by iterating through the string length and counting how many times each character has appeared. When any character is found that appears only once and it is the K'th unique character encountered, it is returned as the result. This operation helps
6 min read