Great article but I'm wondering why 3*n complexity is the best?
Why do you use 3 loops to get the results? It seems we can do this with two loops and 2*n complexity:
functionsquared(arr1,arr2){if(arr1.length!==arr2.length)returnfalseletfrequencyCounter2={}// Create frequencyCounter2for(letnumofarr2){frequencyCounter2[num]=frequencyCounter2[num]+1||1}// Count values from arr1 against frequency counter of arr2for(letvalofarr1){letsquare=val**2if(!(squareinfrequencyCounter2))returnfalse// Decrease frequency counter for our valueletcounter=frequencyCounter2[square]-1if(counter<0)returnfalsefrequencyCounter2[square]=counter}returntrue}
Yeah, your solution looks good, best case complexity is 2n.
I could've used one loop to create both frequency counter objects to reduce it down to 2n, but used two loops as I felt it was easier to see what was going on for readers. In hindsight, perhaps I should've just used the one.
Thanks for reading!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Great article but I'm wondering why 3*n complexity is the best?
Why do you use 3 loops to get the results? It seems we can do this with two loops and 2*n complexity:
Yeah, your solution looks good, best case complexity is 2n.
I could've used one loop to create both frequency counter objects to reduce it down to 2n, but used two loops as I felt it was easier to see what was going on for readers. In hindsight, perhaps I should've just used the one.
Thanks for reading!