Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- PHP
- Haskell
- Clojure
- Ada
- C++
- C#
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Go
- Go
- JS
- JS
- Java
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- Scala
- Scheme
- Smalltalk
unset($items[$i]);
// alters original array $items
take i items ++ drop (1 + i) items
(defn remove-idx [i items]
(keep-indexed #(when-not (= i %1) %2) items))
keep in mind this is iterates the whole collection, but it can be used with more complex predicates
items.erase (items.begin () + i);
items.removeAt(i);
List.delete_at(items, i)
integer, allocatable, dimension(:) :: items
items = [items(:i-1), items(i+1:)]
This uses the Fortran feature of reallocation on assignment.
copy(items[i:], items[i+1:])
items[len(items)-1] = nil
items = items[:len(items)-1]
This code is for pointer value type, and has no memory leak.
let new_list = items.filter(function(val,idx,ary) { return idx != i });
items.remove(i);
Original list is altered.
Be careful not to confuse remove(int) with remove(Object), in case you have a List<Integer> (conversions can be tricky)
Be careful not to confuse remove(int) with remove(Object), in case you have a List<Integer> (conversions can be tricky)
items.remove(i);
items.removeAt(i)
(defun remove-item-by-index (index items)
"Construct a fresh list which is a copy of ITEMS (a list) excluding the item at INDEX."
(loop for item in items
as i from 0
unless (= index i) collect item))
Lisp lists are indexed from 0. Within the LOOP, ITEM is the current item, I is the index of that item, and the two are stepped in parallel.
local removedItem = table.remove(items,i)
alter the original list, return the removed element
list[i] := list[high(list)];
setlength(list,high(list));
2-part Solution:
First the element is overwritten by the last element,
Then the list is reduced by one.
This destroys the original ordering.
First the element is overwritten by the last element,
Then the list is reduced by one.
This destroys the original ordering.
$removed_element = splice @items, $i, 1;
splice alters the original list
val without = {
val (left, right) = items.splitAt(i)
left ++ right.drop(1)
}
(define (removeElementByIndex L i)
(if (null? L)
null
(if (= i 0)
(cdr L)
(cons (car L) (removeElementByIndex (cdr L) (- i 1)))
)
)
)
items removeAt: i.
Assuming items is an OrderedCollection. Other SequenceableCollections do not provide this.