Classic Array & String Algorithms by Hand
Why write them yourself?
Exams and interviews ask for them, and understanding them helps you choose the right STL algorithm later (Unit 14).
1. Linear search — O(n)
Check each element until found. Return the index or "not found" (-1 or std::optional).
2. Find max/min and its position — O(n)
Start with the first element; update when you find a bigger/smaller one.
3. Reverse in place — O(n)
Two indexes from both ends, swap, move inward until they meet.
4. Frequency counting
- Small known range (letters, digits, marks 0–100): an array of counters
int count[26] = {}; count[c - 'a']++; - Arbitrary values:
std::map/std::unordered_map(Unit 13)
5. Remove duplicates
- Sorted data: keep an element only if it differs from the previous one
- Unsorted: a "seen" set or array
6. Second largest — one pass
Track first and second; update both carefully.
7. Check sorted
Compare each element with the next.
8. Prefix sums
prefix[i] = sum of first i elements → the sum of any range [l, r) is prefix[r] - prefix[l] in O(1). Great for running balances.
9. Character classification
std::isalpha, std::isdigit, std::isspace, std::isupper, std::islower (<cctype>)
Analysing your algorithm
- Time: how many steps relative to n?
- Space: extra memory used?
- Edge cases: empty input, one element, all equal, negatives
Example
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
int linear_search(const std::vector<int>& v, int target) {
for (std::size_t i = 0; i < v.size(); ++i)
if (v[i] == target) return static_cast<int>(i);
return -1;
}
void reverse_in_place(std::vector<int>& v) {
for (std::size_t i = 0, j = v.size() - 1; i < j; ++i, --j) std::swap(v[i], v[j]);
}
int second_largest(const std::vector<int>& v) {
int first = v[0], second = -1;
for (int x : v) {
if (x > first) { second = first; first = x; }
else if (x != first && x > second) second = x;
}
return second;
}
std::vector<int> remove_duplicates_sorted(const std::vector<int>& v) {
std::vector<int> out;
for (int x : v)
if (out.empty() || out.back() != x) out.push_back(x);
return out;
}
int main() {
std::vector<int> v = { 42, 7, 93, 15, 93, 61 };
std::cout << "index of 15: " << linear_search(v, 15) << ", index of 100: " << linear_search(v, 100) << '\n';
std::cout << "second largest: " << second_largest(v) << '\n';
reverse_in_place(v);
std::cout << "reversed:";
for (int x : v) std::cout << ' ' << x;
std::cout << '\n';
std::vector<int> sorted = { 1, 1, 2, 3, 3, 3, 7 };
std::cout << "unique:";
for (int x : remove_duplicates_sorted(sorted)) std::cout << ' ' << x;
std::cout << '\n';
std::string text = "Hello BudgetBuddy 2026!";
int letters = 0, digits = 0, upper = 0, count[26] = {};
for (char ch : text) {
unsigned char c = static_cast<unsigned char>(ch);
if (std::isalpha(c)) { ++letters; ++count[std::tolower(c) - 'a']; }
if (std::isdigit(c)) ++digits;
if (std::isupper(c)) ++upper;
}
std::cout << "letters " << letters << ", digits " << digits << ", uppercase " << upper << '\n';
std::cout << "letters used more than once:";
for (int i = 0; i < 26; ++i)
if (count[i] > 1) std::cout << ' ' << static_cast<char>('a' + i) << '=' << count[i];
std::cout << '\n';
// prefix sums for a running balance
std::vector<int> daily = { 500, -200, 1000, -350, 50 };
std::vector<int> prefix(daily.size() + 1, 0);
for (std::size_t i = 0; i < daily.size(); ++i) prefix[i + 1] = prefix[i] + daily[i];
std::cout << "net change days 2-4: " << prefix[4] - prefix[1] << ", final: " << prefix.back() << '\n';
return 0;
}
/* Output:
index of 15: 3, index of 100: -1
second largest: 61
reversed: 61 93 15 93 7 42
unique: 1 2 3 7
letters 16, digits 4, uppercase 3
letters used more than once: b=2 d=3 e=2 l=2 u=2
net change days 2-4: 450, final: 1000
*/