I have an array of items that occur exactly twice, right next to eachother, like so:
const arr = ['1', '1', '2', '2', '3', '3']
I need to sort it in a different order, so all unique values occur first, and then their duplicates, like so:
const arr = ['1', '2', '3', '1', '2', '3']
How can I solve it by using sort()
function? Or is there another way I can achieve this?
I tried something like this, but it didn’t work:
const sorted = myDublicatedArray.sort((left, right) => left.index - right.index);
5
Answers
I can’t think of a way to do this only using
Array.sort()
. The approach below should work for both numeric and string inputs unless there are other requirements:From comments I understand that you will have pairs of duplicate values following one after the other, never 3 or more of the same. In that case, you can map the input array like this:
And if speed is a requirement, then go for an old-fashioned
for
loop:You could maintain an index for every value and get a grouped array as first and then a flat array as result.
If the values are already sorted and values are always present in sets of 2 you could use the following:
The snippet above creates a
Set
uniq
that contains all unique values in the array. To get the desired result we create an array with length 2 withArray.from()
, then useflatMap()
to fill and expand the array. We can useArray.from()
to convertuniq
from aSet
to an array. The item order is based on insertion order ('1'
, then'2'
, then'3'
).If the amount of duplicates can be dynamic (but is the same across the whole array) you could make it more dynamic by using
Array.from({ length: arr.length / uniq.size })
. This would allow the following inputs:Note that the elements are still expected to be sorted and to be consistent in duplicate amount.
You can use
sort
for this. First, map the elements to [element, occurrence] pairs. For example, the first occurrence of 2 is mapped to [2,0].Then, sort first by occurrence, and for elements with equal occurrence, sort by the element itself.
Finally, extract just the element from the [element, occurrence] pair.