This language bar is your friend. Select your favorite languages!

Idiom #165 Last element of list

Assign to variable x the last element of list items.

#include <iterator>
auto x = *std::crbegin(items);
#include<vector>
std::vector<int> items;
int last = items.back();
(last items)
using System.Linq;
var x = items.LastOrDefault();
import std.range;
int[] items;
auto x = items.back();
int[42] items;
int x = items[$-1];
x = items.last;
x = List.last(items)
x = lists:last(items),
x = items(ubound(items,1))
x := items[len(items)-1]
x = last items
foo :: [a] -> Maybe a
foo [] = Nothing
foo xs = Just $ last xs

x = foo items
const x = items[items.length - 1];
int x = items[items.length - 1];
var x = items.last()
local x = items[#items]
$x = $items[array_key_last($items)];
$x = end($items);
x := items[high(items)];
my $x = $items[-1];
my $x = $items[$#items];
x = items[-1]
x = items.last
x = items.last
let x = items.last().unwrap();
let x = items[items.len()-1];
val x = items.takeRight(1)
Dim ItemList As New List(Of String)(New String() {"one", "two", "three"})
Console.WriteLine(ItemList.Last)

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org