Array Functions
Common Pseudocode provides various functions and operations for working with arrays and lists.
Array Properties
Section titled “Array Properties”length - Get Array Length
Section titled “length - Get Array Length”Returns the number of elements in an array.
Syntax:
count = length of arraycount = array.lengthExample:
numbers = [1, 2, 3, 4, 5]count = length of numbers // 5count = numbers.length // 5Adding Elements
Section titled “Adding Elements”append/push - Add Element to End
Section titled “append/push - Add Element to End”Adds an element to the end of an array.
Syntax:
append element to arrayarray.append(element)array.push(element)Example:
numbers = [1, 2, 3]append 4 to numbers // [1, 2, 3, 4]
fruits = ["apple", "banana"]fruits.push("orange") // ["apple", "banana", "orange"]insert - Insert Element at Position
Section titled “insert - Insert Element at Position”Inserts an element at a specific index in an array.
Syntax:
insert element at index position in arrayarray.insert(position, element)Example:
fruits = ["apple", "orange"]insert "banana" at index 1 in fruits // ["apple", "banana", "orange"]
numbers = [1, 3, 4]numbers.insert(1, 2) // [1, 2, 3, 4]Removing Elements
Section titled “Removing Elements”remove - Remove Element at Index
Section titled “remove - Remove Element at Index”Removes an element from an array at a specified index.
Syntax:
remove element at index position from arrayarray.remove(position)Example:
numbers = [1, 2, 3, 4, 5]remove element at index 2 from numbers // [1, 2, 4, 5]
fruits = ["apple", "banana", "orange"]fruits.remove(1) // ["apple", "orange"]Searching
Section titled “Searching”is in - Check if Element Exists
Section titled “is in - Check if Element Exists”Checks whether an element exists in an array.
Syntax:
if element is in arrayExample:
fruits = ["apple", "banana", "orange"]
if "apple" is in fruits output "We have apples!"
if "grape" is in fruits output "We have grapes!"else output "No grapes available"Sorting
Section titled “Sorting”sort - Sort Array
Section titled “sort - Sort Array”Sorts an array in ascending or descending order.
Syntax:
sort array in ascending ordersort array in descending orderarray.sort()array.sort(descending)Example:
numbers = [5, 2, 8, 1, 9]sort numbers in ascending order // [1, 2, 5, 8, 9]
numbers = [5, 2, 8, 1, 9]sort numbers in descending order // [9, 8, 5, 2, 1]
fruits = ["orange", "apple", "banana"]fruits.sort() // ["apple", "banana", "orange"]Array Access
Section titled “Array Access”Index Access
Section titled “Index Access”Access elements using zero-based indexing.
Syntax:
element = array[index]Example:
fruits = ["apple", "banana", "orange"]firstFruit = fruits[0] // "apple"secondFruit = fruits[1] // "banana"thirdFruit = fruits[2] // "orange"Modify Element
Section titled “Modify Element”Change the value of an element at a specific index.
Syntax:
array[index] = newValueExample:
fruits = ["apple", "banana", "orange"]fruits[1] = "grape" // ["apple", "grape", "orange"]Iteration
Section titled “Iteration”for each - Loop Through Array
Section titled “for each - Loop Through Array”Iterate over each element in an array.
Syntax:
for each element in array // code blockExample:
fruits = ["apple", "banana", "orange"]for each fruit in fruits output fruitIndex-based Loop
Section titled “Index-based Loop”Loop through array using indices.
Syntax:
for i from 0 to length of array - 1 // use array[i]Example:
numbers = [10, 20, 30, 40]for i from 0 to length of numbers - 1 output "Index {i}: {numbers[i]}"Multi-dimensional Arrays
Section titled “Multi-dimensional Arrays”Create 2D Array
Section titled “Create 2D Array”Example:
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]Access 2D Array Element
Section titled “Access 2D Array Element”Syntax:
value = array[row][column]Example:
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]value = grid[1][2] // 6 (row 1, column 2)value = grid[0][0] // 1 (row 0, column 0)Loop Through 2D Array
Section titled “Loop Through 2D Array”Example:
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for each row in grid { for each value in row { output value }}Common Array Operations Example
Section titled “Common Array Operations Example”// Create an empty shopping listshoppingList = []
// Add itemsappend "milk" to shoppingListappend "bread" to shoppingListappend "eggs" to shoppingList
// Display listoutput "Shopping List:"for each item in shoppingList { output "- {item}"}
// Check if we need to buy milkif "milk" is in shoppingList output "Don't forget the milk!"
// Remove an itemremove element at index 1 from shoppingList // removes "bread"
// Sort alphabeticallysort shoppingList in ascending order
// Display final listoutput "Final list ({length of shoppingList} items):"for i from 0 to length of shoppingList - 1 { output "{i + 1}. {shoppingList[i]}"}