What is the Efficient way to insert a number into a sorted array of numbers in JavaScript ? Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report Given an array of numbers and the task is to insert a number into the sorted array using JavaScript. There are many approaches to solving this problem two of which are given below: Approach 1: First, take the values in a variable (lets arr).Make sure it is sorted.In this example, the complexity is O(n), where n is the number of elements in the array available.The method findLoc is searching for the element just greater than the element that we want to insert.The method returns the index of the location.perform the insert operation using the .splice() method. Example: This example illustrates the approach discussed above. html <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="gfg_Run()"> Insert </button> <p id="GFG_DOWN"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var today = new Date(); var arr = [1, 2, 4, 6, 9]; el_up.innerHTML = "Click on the button to insert a number "+ "in javascript array.<br> Array is = " + arr; function add(el, arr) { arr.splice(findLoc(el, arr) + 1, 0, el); return arr; } function findLoc(el, arr, st, en) { st = st || 0; en = en || arr.length; for (i = 0; i < arr.length; i++) { if (arr[i] > el) return i - 1; } return en; } function gfg_Run() { add(7, arr); el_down.innerHTML = "Array becomes " + arr; } </script> Output: What is the Efficient way to insert a number into a sorted array of numbers in JavaScript? Approach 2: In this example, the complexity is O(Logn), where n is the number of elements in the array.A method findLoc is searching for the location where the element should be present.Method returns the index of the location by using binary search algorithm.perform the insert operation using .splice() method. Example: This example illustrate the approach discussed above. html <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"></p> <button onclick="gfg_Run()"> Insert </button> <p id="GFG_DOWN"></p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var today = new Date(); var arr = [1, 2, 4, 6, 9]; el_up.innerHTML = "Click on the button to insert a "+ "number in javascript array.<br> Array is = " + arr; function add(el, arr) { arr.splice(findLoc(el, arr) + 1, 0, el); return arr; } function findLoc(el, arr, st, en) { st = st || 0; en = en || arr.length; var pivot = parseInt(st + (en - st) / 2, 10); if (en - st <= 1 || arr[pivot] === el) return pivot; if (arr[pivot] < el) { return findLoc(el, arr, pivot, en); } else { return findLoc(el, arr, st, pivot); } } function gfg_Run() { add(5, arr); el_down.innerHTML = "Array becomes " + arr; } </script> Output: What is the Efficient way to insert a number into a sorted array of numbers in JavaScript? Create Quiz Comment P PranchalKatiyar Follow 1 Improve P PranchalKatiyar Follow 1 Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like