'Sort duplicate numbers from array in Javascript [duplicate]

I had got a question in my exam as follow:

we need to create function that will receive 1 array of all positive number and return all the duplicate values of the array in sorted order.

Here is the solution that I had implemented:

function solution(arr) {
  return arr.filter((value,index)=>arr.indexOf(value)!==index).sort()
}

But the code was rejected. Can someone tell me what could be more optimized solution for this problem in Javascript?



Solution 1:[1]

maybe because of this : solution(['sada','sada','r',4,'r','r']) => ['r', 'r', 'sada'] or solution([2,2,1,3,3,6,7,7,7]) => [2, 3, 7, 7]

your solution when array have same thing more than 2 times, return wrong array .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sadaf Ziya