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)
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With