Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index of number if it was inserted into a sorted array

function getIndexToIns(arr, num) {
  arr.sort(function(a, b) {
    return a - b;
  });
  
  for (var i = 0; i < arr.length; i++) { // cycles through the array
    if (arr[i] >= num) { // if array value is bigger than num 
      return i; // return index pos of num bigger than value
    }
    else if (arr[i] === undefined) { // if not found 
      arr.push(num); // push to array 
      return arr.indexOf(num); // return index pos of new num <-- should return 3 in this case
    }
  }
}

console.log(getIndexToIns([2, 5, 10], 15)); // Should return 3

The mission of this is to sort an array, and return the index value of arg2 if it were in the array.

Example: getIndexToIns([10, 20, 30, 40, 50], 35) should return 3.

What I’m having trouble with, is if arg2 is not found in the array, to push it into it and return its index value. I can’t seem to make it work.

like image 237
tekac Avatar asked May 15 '16 23:05

tekac


People also ask

How do you find the sorted index of an array?

We can get the indices of the sorted elements of a given array with the help of argsort() method.

How do you find the index where a number belongs in an array?

function getIndexToIns(arr, num) { // Sort arr from least to greatest. let sortedArray = arr. sort((a, b) => a - b) // [40, 60]. sort((a, b) => a - b) // [40, 60] // Compare num to each number in sortedArray // and find the index where num is less than or equal to // a number in sortedArray.

How do you find the index of a sorted array in Python?

argsort([1, 2, 3, 100, 5]) yields array([0, 1, 2, 4, 3]) , which appears to be the indices the OP wants. @0 0 your example is a specific case. If we run arr = [1,2,3,100, 5, 9] res = np. argsort(arr) print(res) then we get [0 1 2 4 5 3] which is wrong.

How do you find an index number?

In this method, the index number is equal to the sum of prices for the year for which index number is to be found divided by the sum of actual prices for the base year.


4 Answers

Another way to do it:

function getIndex(arr, num) {
  return arr.concat(num).sort(function(a, b) {
    return a - b;
  }).indexOf(num);
}

Sure there a few ways to do this but the fix in your code is below:

Working Example

function getIndexToIns(arr, num) {
  arr.sort(function(a,b) {
    return a-b;
  });
  for (var i=0;i<arr.length;i++) { // cycles through the array
    if (arr[i] >= num) { // if array value is bigger than num 
        return i; // return index pos of num bigger than value
    }
    if (i === arr.length - 1) { // if not found 
        arr.push(num); // push to array 
        return arr.indexOf(num); // return index pos of new num <-- should return 3 in this case
    }
  }
}

in your code you checked to see if (arr[i] === undefined) that will never happen, so instead check to see if you are at the end of the array, and if so, that means you haven't found your number, and then you can push it and get the index.

like image 129
omarjmh Avatar answered Oct 22 '22 12:10

omarjmh


Why don't you use the .push and .indexOf methods on the array?

function arrSort(a, b) {
    return a - b;
}

function getIndexToIns(arr, num) {
    // you sort the array
    arr.sort(arrSort);

    // if it doesn't contain the num
    if(arr.indexOf(num) == -1) {
        // add the num to the array
        arr.push(num);

        // sort the array again
        arr.sort(arrSort);

        // return the index of the num
        return arr.indexOf(num);
    }

    // if the num is in the array, return its position
    return arr.indexOf(num);
}
like image 32
aifrim Avatar answered Oct 22 '22 11:10

aifrim


Since your arrays seem already sorted, you should just use a dichotomic search to find the index, and then insert it with splice.

function getIndexToIns(arr, num) {
  var index = (function search(from, to) {
    if(from == to) return to;
    var m = Math.floor((from+to)/2);
    if(arr[m] > num) return search(from, m);
    if(arr[m] < num) return search(m+1, to);
    return m;
  })(0, arr.length);
  arr.splice(index, 0, num);
  return index;
}

Or, since it will be linear anyways, loop backwards manually:

function getIndexToIns(arr, num) {
  for(var i=arr.length; i>0 && arr[i-1]>num; --i) arr[i] = arr[i-1];
  arr[i] = num;
  return i;
}
like image 23
Oriol Avatar answered Oct 22 '22 11:10

Oriol


You could just push num into the array then sort it out with map or sort.

function getIndexToIns(arr, num) {
  arr.push(num);
  arr.map(function(a,b) {
    a-b;
     arr.indexOf(num);
  });
  console.log(arr+'\n');
  console.log(num +' is at '+arr.indexOf(num)+'\n');
}

getIndexToIns([2, 5, 10], 15);
like image 20
zer00ne Avatar answered Oct 22 '22 12:10

zer00ne