One dimensional Array in Data Structures with Example
A one-dimensional (1D) array is a linear collection of elements stored in contiguous memory locations. Each element can be accessed directly using its index, which typically starts from 0. It is the simplest and most fundamental data structure, serving as the building block for many other data structures.
Characteristics of One-Dimensional Arrays
- Contiguous Memory: All elements are stored in adjacent memory locations.
- Fixed Size: The size is determined at the time of creation (in static arrays).
- Random Access: Any element can be accessed in O(1) time using its index.
- Homogeneous: All elements in the array are of the same data type.
Declaration and Initialization
In Python (using lists)
# Empty list
arr = []
# Fixed size list (with default values)
arr = [0] * 5 # [0, 0, 0, 0, 0]
# Initialized with values
arr = [10, 20, 30, 40, 50]
# Using list comprehension
arr = [i * 2 for i in range(5)] # [0, 2, 4, 6, 8]
In Java
// Declaration
int[] arr;
// Initialization with size
arr = new int[5]; // [0, 0, 0, 0, 0]
// Declaration and initialization together
int[] arr = {10, 20, 30, 40, 50};
// Using new keyword
int[] arr = new int[]{10, 20, 30, 40, 50};
In C/C++
// Declaration and initialization
int arr[5] = {10, 20, 30, 40, 50};
// Partial initialization
int arr[5] = {10, 20}; // [10, 20, 0, 0, 0]
// Dynamic array (heap)
int* arr = new int[5];
In JavaScript
// Array literal
let arr = [10, 20, 30, 40, 50];
// Using Array constructor
let arr = new Array(5); // [empty × 5]
// Array with values
let arr = Array(10, 20, 30, 40, 50);
Common Operations
1. Traversal
Visiting each element of the array sequentially.
def traverse(arr):
for i in range(len(arr)):
print(arr[i], end=" ")
print()
arr = [10, 20, 30, 40, 50]
traverse(arr) # Output: 10 20 30 40 50
2. Access Element
Retrieving an element using its index (0-based).
def get_element(arr, index):
if 0 <= index < len(arr):
return arr[index]
return None
arr = [10, 20, 30, 40, 50]
print(get_element(arr, 2)) # Output: 30
3. Insertion
Adding an element at a specific position (requires shifting).
def insert_at(arr, index, value):
# Insert value at the given index (0-based)
if index < 0 or index > len(arr):
return arr
arr.insert(index, value)
return arr
arr = [10, 20, 30, 40, 50]
arr = insert_at(arr, 2, 25) # [10, 20, 25, 30, 40, 50]
4. Deletion
Removing an element from a specific position (requires shifting).
def delete_at(arr, index):
if 0 <= index < len(arr):
arr.pop(index)
return arr
arr = [10, 20, 30, 40, 50]
arr = delete_at(arr, 2) # [10, 20, 40, 50]
5. Searching
Finding the index of a target element.
# Linear Search - O(n)
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
# Binary Search - O(log n) (requires sorted array)
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
arr = [10, 20, 30, 40, 50]
print(linear_search(arr, 30)) # Output: 2
print(binary_search(arr, 40)) # Output: 3
6. Updating
Modifying an element at a specific index.
def update(arr, index, new_value):
if 0 <= index < len(arr):
arr[index] = new_value
return arr
arr = [10, 20, 30, 40, 50]
arr = update(arr, 2, 35) # [10, 20, 35, 40, 50]
Complexity Analysis
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Access by index | O(1) | O(1) |
| Update by index | O(1) | O(1) |
| Traversal | O(n) | O(1) |
| Insert at beginning | O(n) | O(1) |
| Insert at end (amortized) | O(1) | O(1) |
| Insert at middle | O(n) | O(1) |
| Delete at beginning | O(n) | O(1) |
| Delete at end | O(1) | O(1) |
| Delete at middle | O(n) | O(1) |
| Linear Search | O(n) | O(1) |
| Binary Search (sorted) | O(log n) | O(1) |
Example: Student Marks Management
# Example: Managing student marks using a 1D array
marks = [85, 90, 78, 92, 88, 76, 95, 82]
# Calculate average
average = sum(marks) / len(marks)
print(f"Average marks: {average:.2f}")
# Find maximum and minimum
max_marks = max(marks)
min_marks = min(marks)
print(f"Highest marks: {max_marks}")
print(f"Lowest marks: {min_marks}")
# Count students above average
above_avg = sum(1 for m in marks if m > average)
print(f"Students above average: {above_avg}")
# Sort marks
marks.sort()
print(f"Sorted marks: {marks}")
# Find student with marks 95
try:
index = marks.index(95)
print(f"Student with marks 95 found at position: {index}")
except ValueError:
print("Student not found")
Advantages & Disadvantages
Advantages
- Simple and easy to understand.
- Fast access (O(1)) to any element.
- Cache-friendly due to contiguous memory allocation.
- Efficient for storing and processing homogeneous data.
- Supports binary search when sorted.
Disadvantages
- Fixed size in many languages (unless using dynamic arrays).
- Insertion and deletion are expensive (O(n)) as elements need to be shifted.
- Memory wastage if the array is not fully utilized.
- All elements must be of the same data type (in static languages).
Applications
- Storing and processing lists of data (e.g., student records, employee IDs).
- Building blocks for other data structures like stacks, queues, heaps.
- Image processing (pixel arrays).
- String manipulation (strings are arrays of characters).
- Sorting and searching algorithms.
- Dynamic programming (e.g., Fibonacci series, knapsack).
- Buffer and cache management.
PreviousMulti dimensional array in Data Structures
Next Suffix Array and Suffix Tree in Data Structures & Applications
Ready to master Data Structures & Algorithms?
Learn DSA hands-on with mentor-led sessions, real interview practice, and placement support.
.png)