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