{"ScriptPreparationCode":"var arr1 = [];\r\nvar arr2 = [];\r\nvar arr3 = [];\r\nvar arr4 = [];\r\n\r\nfor (i = 0; i \u003C 1000; i\u002B\u002B) {\r\n arr1.push(getRandom());\r\n arr2.push(getRandom());\r\n arr3.push(getRandom());\r\n arr4.push(getRandom());\r\n\r\n arr1.sort();\r\n arr2.sort();\r\n arr3.sort();\r\n arr4.sort();\r\n}\r\n\r\nfunction getRandom() {\r\n const minimum = 1;\r\n const maximum = 1000;\r\n var randomnumber = Math.floor(Math.random() * (maximum - minimum \u002B 1)) \u002B minimum;\r\n return randomnumber;\r\n}","TestCases":[{"Name":"Lodash _.intersection()","Code":"const results = new Set(_.intersection(arr1, arr2, arr3, arr4));","IsDeferred":false},{"Name":"Custom optimized function 1 (Best for arbitrary numbers)","Code":"function intersectMultipleArrays(...arrays) {\r\n if (arrays.length === 0) return new Set();\r\n\r\n // Sort arrays by length to optimize operations\r\n arrays.sort((a, b) =\u003E a.length - b.length);\r\n\r\n // Start with the smallest array as the base intersection set\r\n let intersection = new Set(arrays[0]);\r\n\r\n // Iterate over the remaining arrays and filter the intersection\r\n for (let i = 1; i \u003C arrays.length; i\u002B\u002B) {\r\n intersection = new Set(arrays[i].filter(item =\u003E intersection.has(item)));\r\n \r\n // Early exit if intersection is empty\r\n if (intersection.size === 0) return new Set();\r\n }\r\n\r\n // Return the final Set\r\n return intersection;\r\n}\r\n\r\nconst results = intersectMultipleArrays(arr1, arr2, arr3, arr4);","IsDeferred":false},{"Name":"Custom optimized function 2","Code":"const results = new Set(\r\n [arr1, arr2, arr3, arr4]\r\n .sort((a, b) =\u003E b.length - a.length)\r\n .reduce((a, b) =\u003E a.filter(aValue =\u003E b.includes(aValue)))\r\n);","IsDeferred":false},{"Name":"Custom optimized function 3 (Best for reasonable max)","Code":"function intersectMultipleArraysRevised(...arrays) {\r\n let maximum = -1;\r\n for (let i = 0; i \u003C arrays.length; i\u002B\u002B) {\r\n for (let j = 0; j \u003C arrays[i].length; j\u002B\u002B) {\r\n \tconst num = arrays[i][j];\r\n \tif (num \u003E maximum) {\r\n \tmaximum = num;\r\n }\r\n }\r\n }\r\n\r\n const count = new Array(maximum \u002B 1).fill(0);\r\n\r\n for (let i = 0; i \u003C arrays.length; i\u002B\u002B) {\r\n for (let j = 0; j \u003C arrays[i].length; j\u002B\u002B) {\r\n \tconst num = arrays[i][j];\r\n \tcount[num]\u002B\u002B;\r\n }\r\n }\r\n\r\n const result = new Set();\r\n const arrLen = arrays.length;\r\n for (let i = 0; i \u003C= maximum; i\u002B\u002B) {\r\n \tif (count[i] === arrLen) {\r\n \tresult.add(i);\r\n }\r\n }\r\n\r\n return result;\r\n}\r\n\r\nconst results = intersectMultipleArraysRevised(arr1, arr2, arr3, arr4);","IsDeferred":false}]}