{"ScriptPreparationCode":null,"TestCases":[{"Name":"original duplicate removal","Code":"// Remove duplicates that occur 3 or more times in an array\r\n// keeping unique values and those with less than 3\r\nfunction removeMany(arr) {\r\n const newArr = Array.from(arr).sort()\r\n let count = 0;\r\n let result = []\r\n\r\n newArr.forEach((value, index, ar) =\u003E {\r\n count \u002B= 1;\r\n // refactored afterwards from (ar[index \u002B 1] !== value)\r\n if (ar.lastIndexOf(value) \u003C= index \u0026\u0026 count \u003C= 2) {\r\n for (var i = 0; i \u003C arr.length; i\u002B\u002B) {\r\n if (arr[i] === value) {\r\n result.push(arr[i])\r\n }\r\n }\r\n count = 0\r\n } else if (ar[index \u002B 1] !== value) {\r\n count = 0;\r\n }\r\n });\r\n\r\n // \u002B1 is there anyway to return a result that mimicks the original order of \u0060numbers\u0060?\r\n return result; // [1, 2, 2, 3, 4, 4]\r\n}\r\nconst numbers = [1, 2, 3, 2, 4, 4, 5, 5, 5, 5];\r\nconsole.log(removeMany(numbers));","IsDeferred":false},{"Name":"2-pass approach","Code":" // Remove duplicates that occur 3 or more times in an array\r\n // keeping unique values and those with less than 3\r\n function removeMany(arr) {\r\n let countMappings = arr.reduce(function(carry, item) {\r\n if (carry[item]!== undefined) {\r\n carry[item]\u002B\u002B;\r\n }\r\n else {\r\n carry[item] = 1;\r\n }\r\n return carry;\r\n }, {});\r\n return arr.reduce(function(final, item) {\r\n if (countMappings[item] \u003C3) {\r\n final.push(item);\r\n }\r\n return final;\r\n }, []);\r\n }\r\n const numbers = [1, 2, 3, 2, 4, 4, 5, 5, 5, 5];\r\n console.log(removeMany(numbers));","IsDeferred":false},{"Name":"2-pass with filtering","Code":"function removeMany(numbers, max) {\r\n const numberMap = numbers.reduce((map, num) =\u003E {\r\n map[num] = map[num] ? map[num] \u002B 1 : 1;\r\n return map;\r\n }, []);\r\n \r\n return numbers.filter(num =\u003E numberMap[num] \u003C max);\r\n}\r\n\r\nconst numbers = [1, 2, 3, 2, 4, 4, 5, 5, 5, 5];\r\nconsole.log(removeMany(numbers, 3));","IsDeferred":false}]}