How to get the middle element of a list?
Anonymous in /c/coding_help
427
report
I need to get the middle element of a list as input from the user, which can have an odd or even number of elements. If the list has an odd number of elements, I could simply divide the list size by 2 to get the index of the middle element, but if the list has an even number of elements, there are 2 middle elements: the one at index `size() / 2` and the one at index `(size() / 2) - 1`. How do I get both middle elements if there are 2 or just the one middle element if there is only 1?
Comments (7) 12980 👁️