How to remove duplicate array items ? - Where do I Belong

Hey everyone I’m currently working on the Where Do I Belong problem and I’ve managed to push the value into my sorted array, but I can’t figure out an easy way to remove duplicate/multiple occurrences of the same value within an array, since it won’t allow me to pass the question without that. Appreciate any advice I can get :smile:

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  arr.push(num);
  arr.sort();
		for (let i = 0; i < arr.length; i++) {
  		var found = arr.indexOf(num);
  	}
  return found;
}

getIndexToIns([40, 60], 50);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 YaBrowser/18.10.0.2724 Yowser/2.5 Safari/537.36.

Link to the challenge:

OK, you’ve gotten off track here.

You don’t need to push the num onto your array to solve this, but I can see a way to solve it doing that, so I guess that will work. It’s not the most “obvious” solution (at least to me) but it could work.

You need to look at your sort method. The sort method doesn’t work “as is” with numbers. It naturally converts numbers to strings and sorts them as strings, which is different than what you want. But there is a way to fix that with a callback function. You’ve probably encountered it in the lessons and it is easy to google, just look up how to sort an array of numbers in JavaScript.

I’m not sure what you are doing in the for loop. You are just getting the index of num over and over again. How many times do you need to do that? I would ask myself if I need a for loop.

In general, if your indexing variable is nowhere to be found in your for loop, there’s a good chance you don’t need the loop. The exception would be if you want to do something a certain number of times.