I'm pretty new to algorithms and am trying to solve a problem that involves generating a list of 5,000 numbers in random order each time it is run. Each number in the list must be unique and be between 1 and 5,000 (inclusive).
function createRandomList() {
let arr = [];
while(arr.length < 5000){
const num = Math.floor(Math.random() * 5000) + 1;
if(arr.indexOf(num) === -1) arr.push(num);
}
console.log(arr);
}
createRandomList()
Here's the solution that I came up with. I wanted to know the Time/Space complexity of this solution. Would it just be O(1) for both space and time because the values are bounded?
Any feedback would be greatly appreciated as well better ways to optimize the solution.