Array
Space | Time | |||||
---|---|---|---|---|---|---|
Access | Lookup | Insertion | Deletion | |||
O(n) | O(n) | O(n) | O(1) | O(1) |
Definitionβ
- Short
- Detailed
Array is a data structure that stores elements of the same type in contiguous memory locations, allowing for efficient indexing and retrieval.
Simplified
It's like organized row of storage bins where you can put similar items. Each bin has a number, making it easy to find and grab what you need quickly, helping to keep things neat and orderly.
Array is a linear data structure that stores a fixed-size collection of elements in contiguous memory locations. Elements are accessed using numerical indices, allowing for efficient retrieval. Arrays hold elements of the same data type and provide rapid, constant-time access. They are fundamental in algorithms and data structures, forming the basis for more complex structures like matrices.
Advantagesβ
- Fast and efficient access: Arrays have a constant time complexity
O(1)
for indexing, meaning that you can retrieve or update any element in an array with a single operation, regardless of the size of the array - Low memory overhead: Arrays only store the elements and their addresses, without any extra information or pointers, resulting in lower memory usage compared to other data structures
- Ease of implementation: Most programming languages support arrays natively or provide built-in functions and libraries for working with arrays, making them easy to implement and use
Disadvantagesβ
- Fixed size: The number of elements in an array must be predefined, and once declared, the size of the array cannot be changed. This makes insertion and deletion operations trickier, as the array stores elements in consecutive memory locations
- Limited data types: Arrays can only store elements of the same data type, which can be restrictive when working with diverse data sets
Variantsβ
One-Dimensionalβ
Linear data structure that stores a fixed-size, contiguous collection of elements with the same data type.
Two-Dimensionalβ
Structured data format that arranges elements in rows and columns. It is useful for organizing and accessing data in a grid-like fashion, with fixed sizes and efficient storage in contiguous memory locations.
Multi-Dimensionalβ
Multi-dimensional array is a data structure that organizes elements in more than two dimensions. It enables systematic organization and access to data in structured spaces, with fixed sizes along each dimension and contiguous memory storage.
Practiceβ
- Practice
- Solution
Aspect | Pseudo Code |
---|---|
Reverse | Iterate array from the end to the start, appending each element to a new array |
Slice | Creating a new list, then iterating from the start to stop index in the input array, adding each element to the new list |
Concatenate | Combine two arrays into one by first copying elements from the first array, then appending elements from the second array to a new array |
package main
func accessElement(arr []int, index int) int {
return arr[index]
}
func updateElement(arr []int, index, value int) {
arr[index] = value
}
func findLength(arr []int) int {
return len(arr)
}
func iterateElements(arr []int) {
for _, element := range arr {
fmt.Println(element)
}
}
func appendElement(arr []int, value int) []int {
return append(arr, value)
}
func insertElement(arr []int, index, value int) []int {
arr = append(arr[:index], append([]int{value}, arr[index:]...)...)
return arr
}
func deleteElement(arr []int, index int) []int {
return append(arr[:index], arr[index+1:]...)
}
func removeElement(arr []int, value int) []int {
for i := range arr {
if arr[i] == value {
return append(arr[:i], arr[i+1:]...)
}
}
return arr
}
func popElement(arr []int, index int) (int, []int) {
value := arr[index]
return value, append(arr[:index], arr[index+1:]...)
}
func searchElement(arr []int, value int) bool {
for _, element := range arr {
if element == value {
return true
}
}
return false
}
func indexOfElement(arr []int, value int) int {
for i, element := range arr {
if element == value {
return i
}
}
return -1
}
func sortArray(arr []int) {
sort.Slice(arr, func(i, j int) bool {
return arr[i] < arr[j]
})
}
func reverseArray(arr []int) {
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
arr[i], arr[j] = arr[j], arr[i]
}
}
func copyArray(arr []int) []int {
newArr := make([]int, len(arr))
copy(newArr, arr)
return newArr
}
func sliceArray(arr []int, start, stop int) []int {
return arr[start:stop]
}
func concatenateArrays(arr1, arr2 []int) []int {
return append(arr1, arr2...)
}
import java.util.Arrays;
public class ArrayOperations {
public static int accessElement(int[] arr, int index) {
return arr[index];
}
public static void updateElement(int[] arr, int index, int value) {
arr[index] = value;
}
public static int findLength(int[] arr) {
return arr.length;
}
public static void iterateElements(int[] arr) {
for (int element : arr) {
System.out.println(element);
}
}
public static void appendElement(int[] arr, int value) {
arr = Arrays.copyOf(arr, arr.length + 1);
arr[arr.length - 1] = value;
}
public static void insertElement(int[] arr, int index, int value) {
int[] newArr = new int[arr.length + 1];
System.arraycopy(arr, 0, newArr, 0, index);
newArr[index] = value;
System.arraycopy(arr, index, newArr, index + 1, arr.length - index);
arr = newArr;
}
public static void deleteElement(int[] arr, int index) {
int[] newArr = new int[arr.length - 1];
System.arraycopy(arr, 0, newArr, 0, index);
System.arraycopy(arr, index + 1, newArr, index, arr.length - index - 1);
arr = newArr;
}
public static void removeElement(int[] arr, int value) {
int indexToRemove = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
indexToRemove = i;
break;
}
}
if (indexToRemove != -1) {
deleteElement(arr, indexToRemove);
}
}
public static int popElement(int[] arr, int index) {
int value = arr[index];
deleteElement(arr, index);
return value;
}
public static boolean searchElement(int[] arr, int value) {
for (int element : arr) {
if (element == value) {
return true;
}
}
return false;
}
public static int indexOfElement(int[] arr, int value) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == value) {
return i;
}
}
return -1;
}
public static void sortArray(int[] arr) {
Arrays.sort(arr);
}
public static void reverseArray(int[] arr) {
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
public static int[] copyArray(int[] arr) {
return Arrays.copyOf(arr, arr.length);
}
public static int[] sliceArray(int[] arr, int start, int stop) {
return Arrays.copyOfRange(arr, start, stop);
}
public static int[] concatenateArrays(int[] arr1, int[] arr2) {
int[] result = new int[arr1.length + arr2.length];
System.arraycopy(arr1, 0, result, 0, arr1.length);
System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
return result;
}
}
function accessElement(arr, index) {
return arr[index];
}
function updateElement(arr, index, value) {
arr[index] = value;
}
function findLength(arr) {
return arr.length;
}
function iterateElements(arr) {
arr.forEach((element) => {
console.log(element);
});
}
function appendElement(arr, value) {
arr.push(value);
}
function insertElement(arr, index, value) {
arr.splice(index, 0, value);
}
function deleteElement(arr, index) {
arr.splice(index, 1);
}
function removeElement(arr, value) {
const index = arr.indexOf(value);
if (index !== -1) {
deleteElement(arr, index);
}
}
function popElement(arr, index) {
return arr.splice(index, 1)[0];
}
function searchElement(arr, value) {
return arr.includes(value);
}
function indexOfElement(arr, value) {
return arr.indexOf(value);
}
function sortArray(arr) {
arr.sort((a, b) => a - b);
}
function reverseArray(arr) {
arr.reverse();
}
function copyArray(arr) {
return [...arr];
}
function sliceArray(arr, start, stop) {
return arr.slice(start, stop);
}
function concatenateArrays(arr1, arr2) {
return arr1.concat(arr2);
}
fun accessElement(arr: IntArray, index: Int): Int {
return arr[index]
}
fun updateElement(arr: IntArray, index: Int, value: Int) {
arr[index] = value
}
fun findLength(arr: IntArray): Int {
return arr.size
}
fun iterateElements(arr: IntArray) {
arr.forEach { element ->
println(element)
}
}
fun appendElement(arr: IntArray, value: Int): IntArray {
return arr.plus(value)
}
fun insertElement(arr: IntArray, index: Int, value: Int): IntArray {
return arr.sliceArray(0 until index) + value + arr.sliceArray(index until arr.size)
}
fun deleteElement(arr: IntArray, index: Int): IntArray {
return arr.sliceArray(0 until index) + arr.sliceArray(index + 1 until arr.size)
}
fun removeElement(arr: IntArray, value: Int): IntArray {
val index = arr.indexOf(value)
return if (index != -1) {
deleteElement(arr, index)
} else {
arr
}
}
fun popElement(arr: IntArray, index: Int): Pair<Int, IntArray> {
val value = arr[index]
return value to deleteElement(arr, index)
}
fun searchElement(arr: IntArray, value: Int): Boolean {
return arr.contains(value)
}
fun indexOfElement(arr: IntArray, value: Int): Int {
return arr.indexOf(value)
}
fun sortArray(arr: IntArray) {
arr.sort()
}
fun reverseArray(arr: IntArray) {
arr.reverse()
}
fun copyArray(arr: IntArray): IntArray {
return arr.copyOf()
}
fun sliceArray(arr: IntArray, start: Int, stop: Int): IntArray {
return arr.sliceArray(start until stop)
}
fun concatenateArrays(arr1: IntArray, arr2: IntArray): IntArray {
return arr1 + arr2
}
def access_element(arr, index):
return arr[index]
def update_element(arr, index, value):
arr[index] = value
def find_length(arr):
return len(arr)
def iterate_elements(arr):
for element in arr:
print(element)
def append_element(arr, value):
arr.append(value)
def insert_element(arr, index, value):
arr.insert(index, value)
def remove_element(arr, value):
arr.remove(value)
def pop_element(arr, index):
return arr.pop(index)
def search_element(arr, value):
return value in arr
def index_of_element(arr, value):
return arr.index(value)
def sort_array(arr):
arr.sort()
def reverse_array(arr):
arr.reverse()
def slice_array(arr, start, stop):
return arr[start:stop]
def concatenate_arrays(arr1, arr2):
return arr1 + arr2
fn access_element(arr: &[i32], index: usize) -> i32 {
arr[index]
}
fn update_element(arr: &mut [i32], index: usize, value: i32) {
arr[index] = value;
}
fn find_length(arr: &[i32]) -> usize {
arr.len()
}
fn iterate_elements(arr: &[i32]) {
for &element in arr {
println!("{}", element);
}
}
fn append_element(arr: &mut Vec<i32>, value: i32) {
arr.push(value);
}
fn insert_element(arr: &mut Vec<i32>, index: usize, value: i32) {
arr.insert(index, value);
}
fn delete_element(arr: &mut Vec<i32>, index: usize) {
arr.remove(index);
}
fn remove_element(arr: &mut Vec<i32>, value: i32) {
if let Some(index) = arr.iter().position(|&x| x == value) {
arr.remove(index);
}
}
fn pop_element(arr: &mut Vec<i32>, index: usize) -> (i32, Vec<i32>) {
let value = arr.remove(index);
(value, arr)
}
fn search_element(arr: &[i32], value: i32) -> bool {
arr.contains(&value)
}
fn index_of_element(arr: &[i32], value: i32) -> Option<usize> {
arr.iter().position(|&x| x == value)
}
fn sort_array(arr: &mut [i32]) {
arr.sort();
}
fn reverse_array(arr: &mut [i32]) {
arr.reverse();
}
fn copy_array(arr: &[i32]) -> Vec<i32> {
arr.to_vec()
}
fn slice_array(arr: &[i32], start: usize, stop: usize) -> Vec<i32> {
arr[start..stop].to_vec()
}
fn concatenate_arrays(arr1: &[i32], arr2: &[i32]) -> Vec<i32> {
let mut result = arr1.to_vec();
result.extend_from_slice(arr2);
result
}
function accessElement(arr: number[], index: number): number {
return arr[index];
}
function updateElement(arr: number[], index: number, value: number): void {
arr[index] = value;
}
function findLength(arr: number[]): number {
return arr.length;
}
function iterateElements(arr: number[]): void {
arr.forEach((element) => {
console.log(element);
});
}
function appendElement(arr: number[], value: number): void {
arr.push(value);
}
function insertElement(arr: number[], index: number, value: number): void {
arr.splice(index, 0, value);
}
function deleteElement(arr: number[], index: number): void {
arr.splice(index, 1);
}
function removeElement(arr: number[], value: number): void {
const indexToRemove = arr.indexOf(value);
if (indexToRemove !== -1) {
deleteElement(arr, indexToRemove);
}
}
function popElement(arr: number[], index: number): [number, number[]] {
const value = arr.splice(index, 1)[0];
return [value, arr];
}
function searchElement(arr: number[], value: number): boolean {
return arr.includes(value);
}
function indexOfElement(arr: number[], value: number): number {
return arr.indexOf(value);
}
function sortArray(arr: number[]): void {
arr.sort((a, b) => a - b);
}
function reverseArray(arr: number[]): void {
arr.reverse();
}
function copyArray(arr: number[]): number[] {
return [...arr];
}
function sliceArray(arr: number[], start: number, stop: number): number[] {
return arr.slice(start, stop);
}
function concatenateArrays(arr1: number[], arr2: number[]): number[] {
return arr1.concat(arr2);
}