How to find distance between items on array JavaScript?


In this problem statement, our aim is to find the distance between items on an array with the help of Javascript. So for doing this task we will use a loop and a variable to store the id for each object in the JSON object.

Understanding the problem statement

The problem statement is to write a function in Javascript by which we can find the distance between items on an array. If we have an array, the distance will be measured by counting the index numbers from the first item from where we have to measure the distance. For example the array is [ 1, 2, 3, 4, 5 ], let’s say we have to find the distance between 2 to 4 so the distance will be 2 because after 2 there are two elements 3 and 4.

Logic for the given problem

To find the distance between items in an array we will define what we mean by distance. So we can define distance as the number of steps required to move from one item to another item in the array. The distance between two adjacent items will be 1 and the distance between two items that are far apart in the array will be greater than 1. So we will define a function and in this function we will pass three parameters: array, first item and second item. And with the help of the indexOf method we need to find the index of both the items passed in function. For calculating the distance we will find the difference between both the indexes of two items. And return the outcome of distance.

Algorithm

Step 1 − Declare a function to find the distance between two elements and name it findDistance. Pass three parameters in it, arr, item1 and item2.

Step 2 − Find the index of both the items passed in function with the help of indexOf method.

Step 3 − Define a variable called distance in which we will store the distance between the two items. Use Math.abs function and pass the above calculated indices of two elements.

Step 4 − At the end of the function return the distance.

Step 5 − Define array and console the output by calling the function.

Code for the algorithm

//function to calculate the distance between two items
function findDistance(arr, item1, item2) {
   let index1 = arr.indexOf(item1);
   let index2 = arr.indexOf(item2);
   let distance = Math.abs(index1 - index2);
   return distance;
   }
const arr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
console.log("The distance between two items:");
console.log(findDistance(arr, 10, 50));

Complexity

The time taken by the created function is O(n) in which n is the length of the input array. The reason is that the indexOf method is used to search the entire array to find the index of the passed items. And this process needs to be performed two times in the function.

Conclusion

In the above code we have defined distance between items in an array as the number of steps required to move from one item to another in the given array. So we have used the indexOf method to find out the indexes of the two items and calculated the difference between the items. The time complexity is O(n) in which n is the length of the input array.

Updated on: 18-May-2023

889 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements