Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Initialise
Insert
Count
Retrieve by index
Remove by index
Slice
Search
Sort
Compare
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Regex
Strings
Go
Arrays
Maps
Strings
Java
Arrays
Regex
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
C Arrays
Initialise
Fixed contents n/a int a[]={1,2,3,4,5};
1 2 3 4 5
Insert
Prepend element 'e' int a[4]={2,3,4};
e=1;
alen=3;
memmove(
  a+1,
  a,
  sizeof(int)*alen);
a[0]=e;
1 2 3 4
Append element 'e' int a[4]={1,2,3};
e=4;
alen=3;
a[alen]=e;
1 2 3 4
Insert element 'e' at index 'n' int a[6]={1,2,3,4,5};
e=666;
n=3;
alen=5;
memmove(
  a+n+1,
  a+n,
  sizeof(int)*(alen-n));
a[n]=e;
1 2 3 666 4 5
Prepend array 'b' int a[8]={1,2,3,4,5};
int b[]={10,20,30};
alen=5;
blen=3;
memmove(
  a+blen,
  a,
  sizeof(int)*alen);
memcpy(
  a,
  b,
  sizeof(int)*blen);
10 20 30 1 2 3 4 5
Append array 'b' int a[8]={1,2,3,4,5};
int b[]={10,20,30};
alen=5;
blen=3;
memcpy(
  a+alen,
  b,
  sizeof(int)*blen);
1 2 3 4 5 10 20 30
Insert array 'b' at index 'n' int a[8]={1,2,3,4,5};
int b[]={10,20,30};
n=3;
alen=5;
blen=3;
memcpy(
  a+n+blen,
  a+n,
  sizeof(int)*(alen-n));
memcpy(
  a+n,
  b,
  sizeof(int)*blen);
1 2 3 10 20 30 4 5
Concatenate array 'a' and array 'b' int a[]={1,2,3};
int b[]={4,5,6};
alen=3;
blen=3;
int c[6];
memcpy(
  c,
  a,
  sizeof(int)*alen);
memcpy(
  c+alen,
  b,
  sizeof(int)*blen);
1 2 3 4 5 6
Insert element 'e' at indices in 'b' int a[]={1,2,3,4,5};
int b[]={0,2,4};
alen=5;
blen=3;
e=666;
for (n=blen-1;n>=0;--n)
{
  memmove(
    a+b[n]+1,
    a+b[n],
    sizeof(int)*(alen-b[n]));
  a[b[n]]=e;
  alen++;
}
666 1 2 666 3 4 666 5
Overwrite
Overwrite first element int a[]={1,2,3};
a[0]=666;
666 2 3
Overwrite last element int a[]={1,2,3};
alen=3;
a[alen-1]=666;
1 2 666
Overwrite element at index 'n' int a[]={1,2,3};
n=1;
a[n]=666;
1 666 3
Overwrite elements from index 'n' (inclusive) int a[]={1,2,3,4,5,6};
int b[]={7,8,9};
n=2;
blen=3;
memcpy(
  a+n,
  b,
  sizeof(int)*blen);
1 2 7 8 9 6
Count
Size of underlying storage int a[100/sizeof(int)]={1,2,3};
n=sizeof(a);
100
Number of element 'e' in array 'a' int a[]={1,2,2,3,3,3};
e=3;
alen=6;
n=0;
for (i=0;i<alen;++i)
{
  if (a[i]==e) ++n;
}
3
Retrieve by index
Retrieve first element int a[]={1,2,3};
e=a[0];
1
Retrieve last element int a[]={1,2,3};
alen=3;
e=a[alen-1];
3
Retrieve element 'n' int a[]={1,2,3};
n=2;
e=a[n];
3
Remove by index
Remove element at index 'n' int a[]={1,2,3,4,5};
n=2;
alen=5;
memmove(
  a+n,
  a+n+1,
  sizeof(int)*(alen-n-1));
1 2 4 5
Remove elements in index range 'm' to 'n' (inclusive) int a[]={1,2,3,4,5};
m=1;
n=3;
alen=5;
memmove(
  a+m,
  a+n+1,
  sizeof(int)*(n-m+1));
1 5
Remove elements from index 'n' onwards (inclusive) int a[]={1,2,3,4,5};
n=3;
1 2 3
Slice
Slice from index 'm' to 'n' (inclusive) int a[]={1,2,3,4,5};
m=1;
n=3;
alen=5;
memmove(
  a,
  a+m,
  sizeof(int)*(alen-m));
2 3 4
Slice from index 'n' onwards (inclusive) int a[]={1,2,3,4,5};
n=2;
alen=5;
memmove(
  a,
  a+n,
  sizeof(int)*(alen-n));
3 4 5
Slice up to index 'n' (inclusive) int a[]={1,2,3,4,5};
n=3;
1 2 3 4
Search
Test element 'e' 'is in' array int compare_ints_fn (
  const void * a,
  const void * b)
{
  return ( *(int*)a - *(int*)b );
}
int a[]={1,2,3,4,5};
e=3;
alen=5;
qsort(
  a,
  alen,
  sizeof(int),
  compare_ints_fn);
int* p=(int*) bsearch(
  &e,
  a,
  alen,
  sizeof(int),
  compare_ints_fn);
t=(p!=NULL);
1
Find index of unique element 'e' int a[]={1,2,3,4,5};
e=3;
alen=5;
qsort(
  a,
  alen,
  sizeof(int),
  compare_ints_fn);
// If there are multiple elements that match the key,
// the element bsearch returns is unspecified.
int* p=(int*) bsearch(
  &e,
  a,
  alen,
  sizeof(int),
  compare_ints_fn);
n=(p-a);
2
Sort
Sort in ascending order int compare_ints_fn_ascend(
  const void *a,
  const void *b)
{
  // Cast to int* and deref before comparing
  return ( *((int*)b) < *((int*)a) );
}
int a[]={5,1,4,3,2};
alen=5;
qsort(
  a,
  alen,
  sizeof(int),
  compare_ints_fn_ascend);
1 2 3 4 5
Sort in descending order int compare_ints_fn_descend(
  const void *a,
  const void *b)
{
  // Cast to int* and deref before comparing
  return ( *((int*)b) > *((int*)a) );
}
int a[]={5,1,4,3,2};
alen=5;
qsort(
  a,
  alen,
  sizeof(int),
  compare_ints_fn_descend);
5 4 3 2 1
Compare
Test for equality (true) int a[]={1,2,3,4,5};
int b[]={1,2,3,4,5};
int len=5;
t=!(memcmp(
  a,
  b,
  sizeof(int)*len));
1
Test for equality (false) int a[]={1,2,3,4,5};
int b[]={1,2,3,4,50};
int len=5;
t=!(memcmp(
  a,
  b,
  sizeof(int)*len));
0