Skip to content

Conversation

@xingarr
Copy link

@xingarr xingarr commented Nov 4, 2025

Add 4 Practical Array Algorithm Examples

📋 Summary

This PR adds four new comprehensive array algorithm examples to the 03-array chapter, covering common interview questions and real-world use cases. Each example includes both JavaScript and TypeScript implementations with multiple approaches, performance comparisons, and practical examples.

🎯 Motivation

These array algorithms are frequently encountered in:

  • Technical interviews (FAANG and other tech companies)
  • Real-world application development
  • Data processing and manipulation tasks
  • Algorithm learning and practice

The examples fill gaps in the existing array chapter by providing practical, production-ready implementations with detailed explanations.

✨ What's New

1. Array Chunking (11-array-chunking.js/ts)

Split arrays into smaller chunks of a specified size.

Features:

  • 3 different implementation approaches (slice, splice, reduce)
  • Real-world pagination example
  • Edge case handling (empty arrays, chunk size validation)
  • Performance considerations

Use Cases:

  • Pagination systems
  • Batch processing
  • Data visualization (grouping data points)
  • API response chunking

Example:

chunkArray([1, 2, 3, 4, 5, 6, 7], 3)
// Output: [[1, 2, 3], [4, 5, 6], [7]]

2. Flatten Nested Arrays (12-flatten-arrays.js/ts)

Convert multi-dimensional arrays into single-dimensional arrays.

Features:

  • 5 different implementation approaches
    • Built-in flat() method with depth control
    • Recursive approach
    • Reduce method (functional)
    • Iterative approach with stack
  • Performance comparison between all methods
  • Real-world category hierarchy example
  • Support for arbitrary nesting depth

Use Cases:

  • Tree traversal result processing
  • Nested data structure flattening
  • Category/taxonomy flattening
  • Data normalization

Example:

flattenDeep([1, [2, 3], [4, [5, 6]], [[[7]]], 8])
// Output: [1, 2, 3, 4, 5, 6, 7, 8]

3. Remove Duplicates (13-remove-duplicates.js/ts)

Eliminate duplicate values from arrays with multiple strategies.

Features:

  • 6 different implementation approaches
    • Set (most efficient for primitives)
    • Filter with indexOf
    • Reduce method
    • Map method
    • Traditional loop
    • Remove duplicates from objects by property
  • Performance comparison with 10,000 element array
  • Duplicate counting utility function
  • Support for complex objects

Use Cases:

  • Data cleaning and deduplication
  • Unique value extraction
  • User record deduplication
  • Tag/category management

Example:

removeDuplicatesSet([1, 2, 3, 2, 4, 3, 5, 1])
// Output: [1, 2, 3, 4, 5]

removeDuplicatesByProperty(users, 'id')
// Removes duplicate user objects by id property

4. Array Rotation (14-array-rotation.js/ts)

Rotate array elements left or right by k positions.

Features:

  • 5 different implementation approaches
    • Slice and concat
    • Spread operator
    • In-place reversal (most efficient - O(n) time, O(1) space)
    • Simple unshift/pop method
  • Both left and right rotation support
  • Handles k > array.length cases
  • Real-world carousel and scheduling examples
  • Bonus: Find rotation count in rotated sorted arrays

Use Cases:

  • Circular buffers
  • Image carousel navigation
  • Employee shift rotation
  • Game development (circular game boards)
  • Circular queue implementations

Example:

rotateRight([1, 2, 3, 4, 5, 6, 7], 3)
// Output: [5, 6, 7, 1, 2, 3, 4]

rotateLeft([1, 2, 3, 4, 5, 6, 7], 2)
// Output: [3, 4, 5, 6, 7, 1, 2]

📁 Files Added

src/03-array/
├── 11-array-chunking.js       (104 lines)
├── 11-array-chunking.ts       (104 lines)
├── 12-flatten-arrays.js       (137 lines)
├── 12-flatten-arrays.ts       (137 lines)
├── 13-remove-duplicates.js    (169 lines)
├── 13-remove-duplicates.ts    (175 lines)
├── 14-array-rotation.js       (186 lines)
└── 14-array-rotation.ts       (191 lines)

Total: 8 new files, ~1,200 lines of well-documented code

🎓 Educational Value

Each example includes:

  • Multiple approaches - Learn different ways to solve the same problem
  • Time/Space complexity considerations - Understand performance trade-offs
  • Real-world examples - See practical applications
  • Edge cases - Learn defensive programming
  • Performance comparisons - Measure actual execution times
  • Best practices - Follow modern JavaScript/TypeScript patterns
  • Detailed comments - Understand the "why" behind the code

🧪 Testing

All examples have been manually tested and verified:

  • ✅ JavaScript versions run successfully with Node.js
  • ✅ TypeScript versions compile and run correctly
  • ✅ All edge cases handled properly
  • ✅ Performance comparisons execute without errors
  • ✅ Real-world examples produce expected output

Test commands:

node src/03-array/11-array-chunking.js
node src/03-array/12-flatten-arrays.js
node src/03-array/13-remove-duplicates.js
node src/03-array/14-array-rotation.js

📊 Performance Highlights

Based on testing with large arrays (10,000 elements):

Remove Duplicates:

  • Set method: ~0.2ms ⚡ (fastest)
  • Map method: ~0.4ms
  • Loop method: ~4ms
  • Reduce method: ~6ms
  • Filter method: ~7ms

Flatten Arrays:

  • All methods perform well for reasonable nesting depths
  • Built-in flat() is optimized and recommended
  • Iterative approach good for very deep nesting

Array Rotation:

  • In-place reversal: O(n) time, O(1) space ⚡ (most efficient)
  • Slice/concat methods: O(n) time, O(n) space (most readable)

🔄 Consistency with Existing Code

These examples follow the established patterns in the repository:

  • ✅ Same file naming convention (##-description.js/ts)
  • ✅ Consistent code style and formatting
  • ✅ Similar comment structure and documentation
  • ✅ Console output with clear section headers
  • ✅ Command to run at the end of each file
  • ✅ Both JS and TS versions provided

🚀 How to Use

Run any example individually:

# JavaScript versions
node src/03-array/11-array-chunking.js
node src/03-array/12-flatten-arrays.js
node src/03-array/13-remove-duplicates.js
node src/03-array/14-array-rotation.js

# TypeScript versions (if ts-node is installed)
ts-node src/03-array/11-array-chunking.ts
ts-node src/03-array/12-flatten-arrays.ts
ts-node src/03-array/13-remove-duplicates.ts
ts-node src/03-array/14-array-rotation.ts

Or navigate to the directory:

cd src/03-array
node 11-array-chunking.js

💡 Future Enhancements

Potential follow-up additions:

  • Jest test cases for each algorithm
  • Additional array algorithms (two-pointer, sliding window)
  • LeetCode problem mappings
  • Complexity analysis documentation
  • Interactive examples/visualizations

📚 References

These implementations are based on:

  • Common interview question patterns
  • JavaScript/TypeScript best practices
  • Real-world production code patterns
  • Algorithm textbook approaches

✅ Checklist

  • Code follows repository style guidelines
  • Both JavaScript and TypeScript versions provided
  • All examples tested and verified working
  • Comprehensive comments and documentation
  • Real-world use cases included
  • Edge cases handled
  • Performance considerations addressed
  • No breaking changes to existing code

🤝 Contributing

These examples are ready for:

  • Code review
  • Suggestions for additional approaches
  • Performance optimization ideas
  • Additional real-world examples
  • Test case contributions

📝 Notes for Reviewers

  • All code is production-ready and follows best practices
  • TypeScript types are properly defined with generics
  • No external dependencies added
  • Examples are self-contained and can run independently
  • Performance comparisons use console.time() for simplicity

Ready to merge! 🎉

These additions significantly enhance the array chapter with practical, interview-ready algorithms that learners will find immediately useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant