Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Time Complexity of creating set from array?

I want to find time complexity of following code. I have an array of integer having duplicate values. I created set from array to remove duplicate entries and then initialize new array from that set using spread operator.

Code:

let list=[1,1,2,3,4,4]
let uniqueNumbers=[...new Set(list)]
console.log(uniqueNumbers)
like image 452
Asad Gulzar Avatar asked Oct 19 '25 03:10

Asad Gulzar


1 Answers

There are 2 things being done here:

  • new Set(list) iterates over every element of the list and puts it into a Set. This is O(n)
  • [...set] iterates over every element of the Set and puts it into an array. This will also be O(n) in nearly all cases.

Both operations are O(n), so overall, the computational complexity is O(n).

like image 161
CertainPerformance Avatar answered Oct 20 '25 17:10

CertainPerformance



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!