Click here to Skip to main content
15,397,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
JavaScript
function func(x, y){
    return x.filter((i) => y.indexOf(i) != -1);
}

let x= ['a','b','c','b','a'];
let y= ['a','b','z'];

console.log(func(x, y));


What I have tried:

My assumption is:
Time complexity o(n^2) : As filter will be o(n) and inside filter indexOf is used that’s another o(n) making total o(n^2)
But as filter and indexOf is on different arrays, should we say o(n*m) or o(n^2) is fine?
and space complexity o(n) -> as I assumed only space required is to store 2 arrays and function. Inside function nothing is stored its just returning results. Will it be right to say o(n) fir 2 arrays and one function?
Posted
Updated 8-May-22 8:29am
v2

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
   
Comments
Black Mamba Elapidae 8-May-22 10:08am
   
@OriginalGriff : Thank you for your time to comment here. Sorry for just writing my assumption and not the thought process behind it, I have updated comment below question to explain it.

Lastly, No, I dont get paid for calculating complexity of simple code and also this is not related academic homework... I am trying to learn complexity as it is widely asked in interviews and I am just practicing it for common code I normally write.. I already did lot of google search before posting question here... And my question was more of a “yes” “no” type, as I already wrote my assumption on answer and just asked if its correct or not.. never asked for step by step answer! Also I saw article link you posted and didn’t find it relevant. If you still feel this question does not deserve answer then you can ignore it :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900