Find Middle Elements from an Array

Javascript: Finding the most middle value in an array

If you have an array with for example five items, the middle item is at index two:

var arr = [ item, item, middle, item, item];

Dividing the length by two and using Math.round would give you index three rather than two, so you would need to subtract one from the length first:

var middle = arr[Math.round((arr.length - 1) / 2)];

You say in your question that you are supposed to use Math.round, but if that is not a requirement, you can get the same result easier using Math.floor:

var middle = arr[Math.floor(arr.length / 2)];

For an array with an even number of items, that will give you the second of the two items that are in the middle. If you want the first instead, use Math.floor and substract one from the length. That still gives the same result for odd number of items:

var middle = arr[Math.floor((arr.length - 1) / 2)];

Get the middle two items of an array with even number of items

The logic is pretty easy:

  • If array is empty, don't return anything
  • If array has odd number of items, the index of the middle item is simply Math.floor(arr.length / 2)
  • If array has even number of items, the index of the two middle items are arr.length / 2 - 1 and arr.length / 2 respectively

See proof-of-concept example:

function middleItem(arr) {
if (!arr.length)
return;

const mid = arr.length / 2;
if (arr.length % 2 === 1) {
return arr[Math.floor(mid)];
} else {
return [
arr[mid - 1],
arr[mid],
];
}
}

console.log(middleItem([])); // undefined
console.log(middleItem([0])); // 0
console.log(middleItem([0,1])); // [0, 1]
console.log(middleItem([0,1,2])); // 1
console.log(middleItem([0,1,2,3])); // [1, 2]
console.log(middleItem([0,1,2,3,4])); // 2

how to find middle element of array in javascript?

You can do it using recursion:

var array = [1,2,8,4,5];

function middle(i) {
if (array[i] !== undefined) {
return middle(i+1);
} else {
return array[Math.floor(i / 2)];
}
}

console.log(middle(0));

Finding the middle element of an array

Write a method like :

void printMiddleofArray(String[] arrayOfNames) {
if (arrayOfNames.length %2 ==0)
{
System.out.println(arrayOfNames[arrayOfNames.length /2]);
System.out.println(arrayOfNames[(arrayOfNames.length /2)-1]);
} else {
System.out.println(arrayOfNames[(arrayOfNames.length /2)-1]);
}
}

find middle elements from an array

This is quick, not tested but the basic idea...

const int n = 5;

// Get middle index
int arrLength = sizeof(myArray) / sizeof(int);

int middleIndex = (arrLength - 1) / 2;
// Get sides
int side = (n - 1) / 2;

int count = 0;

int myNewArray[n];

for(int i = middleIndex - side; i <= middleIndex + side; i++){
myNewArray[count++] = myArray[i];
}

finding Middle element of an array in javascript

This should get you somewhere (from this SO answer):

if (nums.length %2 == 0) {
// even-length array (two middle elements)
var avg = (nums[(nums.length/2) - 1] + nums[nums.length/2]) /2;
}

How to get the middle element in array?

Like this:

int mid = nums[nums.Length/2];

You take the size of the array (nums.Length), divide by two to get the index in the middle and use that index.



Related Topics



Leave a reply



Submit