function three(arr){ return arr.filter((el, i)=> { var s = 0; while (i--) if (el == arr[i]) s++; return (s>1&&s<=2); }); } alert(three([9, 9, 9, 111, 5, 2, 3, 4, 4, 5, 5,5,7]));