View Subcategory
Remove an element from a list by index
Given the list
[Apple, Banana, Carrot], remove the first element to produce the list [Banana, Carrot]
ruby
['Apple', 'Banana', 'Carrot'].shift
fruit.delete_at(0)
java
list.remove(0);
perl
@list = qw(Apple Banana Carrot);
shift @list;
shift @list;
@list = qw(Apple Banana Carrot);
$offset = 0;
splice(@list, $offset, 1);
$offset = 0;
splice(@list, $offset, 1);
groovy
// to produce a new list
newlist = list.tail() // for 'Apple' at start
newlist = list - 'Apple' // for 'Apple' anywhere
newlist = list.tail() // for 'Apple' at start
newlist = list - 'Apple' // for 'Apple' anywhere
// mutate original list
list.remove(0)
list.remove(0)
scala
val (fl, fr) = fruit.splitAt(0) ; fruit = fl ::: fr.tail
fruit = fruit.tail
fruit = fruit.drop(1)
fruits = fruits.remove(fruits.indexOf(_) == 0)
python
myList = ['Apple', 'Banana', 'Carrot']
print myList
del myList[0]
# or
myList.pop(0) # returns 'Apple'
print myList
print myList
del myList[0]
# or
myList.pop(0) # returns 'Apple'
print myList
cpp
fruit->RemoveAt(0);
fsharp
let split_at list n =
let rec split_at' list' n' left right =
match list' with
| [] -> (List.rev left, List.rev right)
| x :: xs -> if n' <= n then split_at' xs (n' + 1) (x :: left) right else split_at' xs (n' + 1) left (x :: right)
split_at' list 0 [] []
// ------
let (_, right) = split_at fruit 0
let rec split_at' list' n' left right =
match list' with
| [] -> (List.rev left, List.rev right)
| x :: xs -> if n' <= n then split_at' xs (n' + 1) (x :: left) right else split_at' xs (n' + 1) left (x :: right)
split_at' list 0 [] []
// ------
let (_, right) = split_at fruit 0
let drop list n =
if n <= 0 then
list
else
let (_, right) = split_at list (n - 1)
right
// ------
let result = (drop fruit 1)
if n <= 0 then
list
else
let (_, right) = split_at list (n - 1)
right
// ------
let result = (drop fruit 1)
erlang
Result = tl(List),
[_|Result] = List,
N = 1, {Left, Right} = lists:split(N - 1, List), Result = Left ++ tl(Right),
Result = drop(1, List),
ocaml
let delete_at i al =
if i < 0 || i >= List.length al then
invalid_arg "delete_at"
else
let rec del i l =
match l with
| [] -> []
| h::t when i = 0 -> t
| h::t -> h :: del (i-1) t
in
del i al
;;
if i < 0 || i >= List.length al then
invalid_arg "delete_at"
else
let rec del i l =
match l with
| [] -> []
| h::t when i = 0 -> t
| h::t -> h :: del (i-1) t
in
del i al
;;
let rem_first l =
match l with
| [] -> []
| h::t -> t
;;
match l with
| [] -> []
| h::t -> t
;;
List.tl ["Apple"; "Banana"; "Carrot"]
csharp
class Solution1516
{
static void Main()
{
List<string> fruit = new List<string>() { "Apple", "Banana", "Carrot" };
fruit.RemoveAt(0);
}
}
{
static void Main()
{
List<string> fruit = new List<string>() { "Apple", "Banana", "Carrot" };
fruit.RemoveAt(0);
}
}
php
$list = array("Apple", "Banana", "Carrot");
unset($list[0]);
// Be aware of that $list[0] isn't set. "Banana" is still $list[1]
unset($list[0]);
// Be aware of that $list[0] isn't set. "Banana" is still $list[1]
$list = array("Apple", "Banana", "Carrot");
array_shift($list);
// Be aware of that $list[0] is set to "Banana"
array_shift($list);
// Be aware of that $list[0] is set to "Banana"
haskell
deleteNth n xs | n > 0 = take (n-1) xs ++ drop n xs
main = print $ deleteNth 1 [1..3]
main = print $ deleteNth 1 [1..3]
fruit :: [String]
fruit = ["Apple", "Banana", "Carrot"]
main :: IO ()
main = putStrLn $ show $ tail fruit
fruit = ["Apple", "Banana", "Carrot"]
main :: IO ()
main = putStrLn $ show $ tail fruit
clojure
(let [fruit ["Apple" "Banana" "Carrot"]
index 0]
(concat
(take index fruit)
(drop (+ index 1) fruit)))
index 0]
(concat
(take index fruit)
(drop (+ index 1) fruit)))
fantom
list := ["Apple", "Banana", "Carrot"]
list.removeAt(0)
list.removeAt(0)
go
offset := 0
list = append(list[:offset], list[offset+1:]...)
list = append(list[:offset], list[offset+1:]...)
Remove the last element of a list
ruby
list = ['Apple', 'Banana', 'Carrot']
list.delete_at(-1)
list.delete_at(-1)
list = ['Apple', 'Banana', 'Carrot']
list.pop
list.pop
java
list.remove(list.size() - 1);
perl
pop @list;
groovy
list = ['Apple', 'Banana', 'Carrot']
// to produce a new list
newlist = list[0,1]
// to modify original list
list.remove(2)
// to produce a new list
newlist = list[0,1]
// to modify original list
list.remove(2)
scala
fruit = fruit.init
fruit = fruit.take(fruit.length - 1)
python
myList = ['Apple', 'Banana', 'Carrot']
myList.pop()
myList.pop()
cpp
fruit->RemoveAt(fruit->Count - 1);
fsharp
let take list n =
if n <= 0 then
list
else
let (left, _) = split_at list (n - 1)
left
// ------
let result = (take fruit ((List.length fruit) - 1))
if n <= 0 then
list
else
let (left, _) = split_at list (n - 1)
left
// ------
let result = (take fruit ((List.length fruit) - 1))
let but_last list =
let rec but_last' list' acc =
match list' with
| [x] -> List.rev acc
| x :: xs -> but_last' xs (x :: acc)
if List.is_empty list then [] else but_last' list []
// ------
let result = (but_last fruit)
let rec but_last' list' acc =
match list' with
| [x] -> List.rev acc
| x :: xs -> but_last' xs (x :: acc)
if List.is_empty list then [] else but_last' list []
// ------
let result = (but_last fruit)
erlang
Result = init(List),
Result = take(length(List) - 1, List),
Result = lists:reverse(tl(lists:reverse(List))),
ocaml
let remove_last list =
match (List.rev list) with
| h::t -> List.rev t
| [] -> []
match (List.rev list) with
| h::t -> List.rev t
| [] -> []
let remove_last lst =
List.rev (List.tl (List.rev lst))
List.rev (List.tl (List.rev lst))
let list_remove_last l =
let rec aux h q acc =
match q with
| [] -> List.rev acc
| h2 :: q -> aux h2 q (h :: acc) in
match l with
| [] -> invalid_arg "list_remove_last"
| h :: q -> aux h q []
let rec aux h q acc =
match q with
| [] -> List.rev acc
| h2 :: q -> aux h2 q (h :: acc) in
match l with
| [] -> invalid_arg "list_remove_last"
| h :: q -> aux h q []
csharp
List<string> fruits = new List() { "apple", "banana", "cherry" };
fruits.RemoveAt(fruits.Length - 1);
fruits.RemoveAt(fruits.Length - 1);
php
$list = array("Apple", "Banana", "Carrot");
unset($list[count($list)-1]);
// Be aware of that
// $list[] = "Orange";
// will be $list[3] and not $list[2]
unset($list[count($list)-1]);
// Be aware of that
// $list[] = "Orange";
// will be $list[3] and not $list[2]
$list = array("Apple", "Banana", "Carrot");
array_pop($list);
array_pop($list);
haskell
ages = [1,2,3,4]
init ages
init ages
clojure
(pop ["Apple" "Banana" "Carrot"])
fantom
list := ["Apple", "Banana", "Carrot"]
list.removeAt(-1)
list.removeAt(-1)
list := ["Apple", "Banana", "Carrot"]ยจ
list.pop
list.pop
Rotate a list
Given a list
["apple", "orange", "grapes", "bananas"], rotate it by removing the first item and placing it on the end to yield ["orange", "grapes", "bananas", "apple"]
ruby
items = ["apple", "orange", "grapes", "bananas"]
items << first = items.shift
# items is rotated
# first contains the first value in the list
items << first = items.shift
# items is rotated
# first contains the first value in the list
java
list.add(list.remove(0));
Collections.rotate(list, -1);
perl
@list = qw(apple, orange, grapes, bananas);
push @list, shift @list;
push @list, shift @list;
@list = qw(apple orange grapes bananas);
@list = @list[1..$#list,0];
@list = @list[1..$#list,0];
groovy
first = items.head()
items = items.tail() + first
items = items.tail() + first
items = items[1..-1] + items[0]
items = items + items.remove(0)
scala
items = items.tail ::: List(items.head)
items = (items.head :: ((items.tail).reverse)).reverse
python
l = ["apple", "orange", "grapes", "bananas"]
first, l = l[0], l[1:] + l[:1]
first, l = l[0], l[1:] + l[:1]
fruit = ['apple', 'orange', 'grapes', 'bananas']
fruit.append(fruit.pop(0))
fruit.append(fruit.pop(0))
cpp
fruit->Add(fruit[0]); fruit->RemoveAt(0);
rotate(fruit.begin(), fruit.begin()+1, fruit.end());
fsharp
let rotate list n =
if n <= 0 then
list
else
let (left, right) = split_at list (n - 1)
right @ left
// ------
let result = (rotate fruit 1)
if n <= 0 then
list
else
let (left, right) = split_at list (n - 1)
right @ left
// ------
let result = (rotate fruit 1)
erlang
N = 1, {Left, Right} = lists:split(N, List), Result = Right ++ Left,
N = 1, Result = rotate(N, List),
ocaml
let rotate list =
match list with
| head::tail -> tail@[head]
| [] -> []
match list with
| head::tail -> tail@[head]
| [] -> []
csharp
var lst = new LinkedList<String>(new String[] {"apple", "orange", "grapes", "banana"});
lst.AddLast(lst.First());
lst.DeleteFirst();
lst.AddLast(lst.First());
lst.DeleteFirst();
php
$list = array("Apple", "Orange", "Grapes", "Banana");
$first = array_shift($list); //get and remove the first
array_push($list, $first); //prepend the $first to the array
$first = array_shift($list); //get and remove the first
array_push($list, $first); //prepend the $first to the array
haskell
main = print $ rotate ["apple", "orange", "grapes", "bananas"]
rotate xs | length xs < 2 = xs
| otherwise = tail xs ++ [head xs]
rotate xs | length xs < 2 = xs
| otherwise = tail xs ++ [head xs]
clojure
(let [fruit ["apple" "orange" "grapes" "bananas"]]
(concat (rest fruit) [(first fruit)])
(concat (rest fruit) [(first fruit)])
fantom
list := ["apple", "orange", "grapes", "bananas"]
list.add(list.removeAt(0))
list.add(list.removeAt(0))
