Coding: Custom Sorting Algorithm- Ascending for Unique Elements, Descending for Duplicates in JavaScript

Monu Kumar Modi
JavaScript in Plain English
3 min readDec 15, 2023

--

When working with arrays in JavaScript, you may need to sort the unique and duplicate elements in the array. In this tutorial, we will learn how to sort unique elements in ascending order and duplicate elements in descending order.
Given an array of integers, we need to sort the unique elements in ascending order and the duplicate elements in descending order. For example, if the input array is [2, 1, 9, 1, 2, 5, 1, 6, 1], the output should be [5, 6, 9, 2, 2, 1, 1, 1, 1].

Introduction: Sorting arrays is a basic activity in programming that frequently requires a customized method. Today, we explore a custom sorting algorithm that gracefully manages duplicates by sorting them in descending order, in addition to arranging unique pieces in ascending order. Together, we will explore the implementation of this algorithm and how it might expand your toolkit for manipulating arrays as we continue on a journey through code.

Solution :

custom sort

Understanding the Code

Step1: Counting Occurrence

The algorithm begins by counting the occurrence of each element using an object (counts). This step sets the stage for distinguishing between unique and duplicate elements.

Step 2: Separation

Next, the array is traversed again, and elements are separated into two arrays: uniqueElement for elements with a count of 1 and duplicateElement for elements with counts greater than 1.

Step 3: Sorting

The unique elements are sorted in ascending order, while the duplicate elements are sorted in descending order.

Step 4: Concatenation

Finally, the two arrays are concatenated to form the sorted result.

Explanation

The customSort function takes an array as input and returns the sorted array. Here’s how it works:

  1. We first create an object counts to store the occurrence of each element in the array.
  2. We then loop through the array and update the counts object with the occurrence of each element.
  3. We then loop through the array again and separate the unique elements and duplicate elements into two separate arrays.
  4. We sort the unique elements in ascending order using the sort method.
  5. We sort the duplicate elements in descending order using the sort method and passing a custom comparison function.
  6. Finally, we concatenate the unique and duplicate elements and return the sorted array.

Complete Code:

function customSort(arr) {
const counts = {}
// Occurence of Each Element
arr.forEach((num) => {
counts[num] = (counts[num] || 0) + 1
})

// Separate Unique Elements and duplicate elements
const uniqueElement = []
const duplicateElement = []

arr.forEach((num) => {
if (counts[num] === 1) {
uniqueElement.push(num)
} else {
duplicateElement.push(num)
}
})

// Sort unique elements in ascending order
uniqueElement.sort((a, b) => a - b)

// Sort duplicate elements in descending order
duplicateElement.sort((a, b) => b - a)

// Concatenate unique elements and duplicate elements
const result = uniqueElement.concat(duplicateElement)

return result
}

const nums = [2, 1, 9, 1, 2, 5, 1, 6, 1]
const result = customSort(nums)
console.log(result)

Conclusion

Custom sorting algorithms provide a powerful tool for tailoring array manipulation to specific needs. In this exploration, we’ve witnessed how a carefully crafted algorithm can efficiently handle sorting unique and duplicate elements in a single array. Incorporate this technique into your coding toolkit for enhanced array sorting capabilities.

I hope you found this tutorial helpful. If you have any questions or comments, please feel free to leave them.

If you’re interested in learning more about coding in Javascript, be sure to follow me for more code snippets and examples. I’ll be sharing valuable information and tips on how to master the language and improve your skills as a developer. So, stay tuned for more updates, and let’s continue learning together.

Thanks for reading.Happy learning 😄

PlainEnglish.io 🚀

Thank you for being a part of the In Plain English community! Before you go:

--

--