Is my code efficient? Where do I Belong(Basic Algorithm Scripting)

Here is the problem, Basic Algorithm Scripting: Where do I Belong
Here is my code,

function getIndexToIns(arr, num) {
  var sortArr = arr.sort(function(a,b){return a-b});
  var perfectNum = 0;
  for(let i=0; i < sortArr.length; i++){
    if(arr[i] >= num){
      perfectNum = i;
      break;
    } else if(num > sortArr[arr.length-1]){
      perfectNum = arr.length;
    }
  }
  return perfectNum;
}

Is my way of solving this issue is efficient? Feel free to share if you have any better process? :hugs:

Some Useful link:

  1. Array.prototype.reduce()

  2. Array.prototype.filter()

  3. Array.prototype.length

1 Like

Sure, that is a good basic solution. Is it the best solution? That is subjective, but there is certainly a more efficient solution. Can you imagine a way to do this without sorting, making only one pass through the data?

1 Like
2 Likes

Thinking is just blowing my mind :heart_eyes: :star_struck:
Is that more efficient?

function getIndexToIns(arr, num) {
  var count = 0;
  var cool = 0;
  for(let i = 0; i < arr.length; i++){
    if(arr[i] <= num){
      count += 1;
      if(arr[i] === num){
        cool += 1;
      }
    }
  }
  if(cool === 0){
    return count;
  } else{
    return count-cool;
  }
}

I think you can still tighten up the code even more. You don’t need cool or a nested if if you change your if condition.

1 Like

the fact that you remove cool it may tell you that you actually don’t need it, and need to make a different adjustment

3 Likes

Right, I was able to solve this with one variable, one loop, and one if, along the lines of:

let count = 0;
for // yada yada yada
  if (/* some condition */) 
    // some action
2 Likes

Mildly related - Is there a performance impact/general preference between using a reduce vs a filter + length in JavaScript?

I know in some languages both approaches provide the same performance (Rust) while other languages produce less efficient code for filter + length (which is a shame because it looks cleaner/more readable).

1 Like

I wouldn’t expect a difference, but it also may be engine dependent because I think the spec just specifies the behavior, not the implementation. And JS isn’t exactly a fast language anyway so I think most of the time you don’t worry micro-optimizations like this. Just my $.02.

2 Likes

Okay :hugs:

function getIndexToIns(arr, num) {
  let count = 0;
  for(let i = 0; i < arr.length; i++){
    if(arr[i] < num){
      count += 1;
    }
  }
return count;
}
2 Likes

There you go! If you are feeling up for a challenge, you can try to formulate that same logic using high order methods, with either a reduce or a filter + length.

1 Like

Yeah, I think reduce is the “sexiest” solution. But the filter/length solution is good to figure out.

2 Likes

I think it depends upon the community which is sexier?

Generally Rustations prefer

let arr = [40, 60];
let num = 50;
let index = arr.iter().filter(|x| x < num).count();

so that’s where my training/mind is at.

1 Like

Yeah, I mean it is very dependent on language. But to me that is semantically what reduce is for - reducing an array into a single value. I prefer the reduce, but I also wouldn’t kick your solution out of bed for eating crackers.

2 Likes

Yeah, it took me a while to get used to. I started writing those as reduces but soon saw that most codes used filter+count in that community. Weird how languages pick up quirks like that.

1 Like

I’m a big fan of .reduce, but in this case, what could be more sexy than a solution that eliminates the necessity to sort the array first? I’m not sure how to do this purely with .reduce, without a chain .sort().reduce(). But then again I’m maybe just easily impressed by fancy one-liners like

return arr.filter(e => e < num).length.

Oh, you can reduce with a ternary if you like those

return arr.reduce((total, elem) => total + (elem < num ? 1 : 0), 0)

That’s so hot. Totally escaped me.

Of course, you could also simplify a tad more by coercing the boolean to a number with the unary plus operator,

total + (elem < num ? 1 : 0)

becomes

total + +(elem < num)

But at some point things start getting unnecessarily arcane, sacrificing readability for cuteness.

4 Likes

The fine balance between impressing and annoying your co-workers and/or future self.