Reject Arrays : Choose the Best Data Structure for Your Code

Reject Arrays : Choose the Best Data Structure for Your Code

Hey folks, long time no see! I'm back with a new blog post, so let's jump right in!

Today I will cover why you should not always use an array as your data structure and switch up based on the use case.

I have seen some of my friends and colleagues use arrays all the time as their primary data structure. It is not wrong considering that arrays are the most widely used data structures for day-to-day programming. But as an effective software developer, you should always consider the data structure based on the application.

Here are a few examples where replacing an array with another popular data structure is a good practice.

Modifying data in place:

As we know, an array is declared with a size, which means changing the size of the array is not possible. The only solution you are left with is to copy the old array into a new array. This is not fun! Instead what you can do is use a linked list data structure.

Linked lists provide an easy way out in such scenarios. As the size is never an issue with linked lists, we just declare a new node and insert it anywhere we need to. It is time and space effective.

Too many duplicate values:

Imagine a scenario where there are a lot of duplicate values coming your way. If you do not care about the duplicate values, you can switch the array for a set. Set stores unique values only, meaning you will save a lot of space if you use sets. Note that if you need access to all duplicate values, this may not be a good idea. You may get fired. Don't blame me, I'm from Uranus.

Frequent sorting and searching:

Using arrays you can perform a binary search but even then you need to sort the elements first. Rather, you can use a binary search tree. A binary search tree has an important property. The in-order traversal of a binary search tree is always sorted. You can also incorporate hash maps in your code if you need timely searches. These are great alternatives to the usual and time-consuming array.

Irregular and multi-dimensional shape:

Let's be honest. Arrays are great till they are 2D. It gets harder to visualize and implement as the dimension grows. Trees or graphs are a simpler and more efficient way to store irregular and multi-dimensional data. You can check out my previous blog post that goes over trees for beginner programmers.

Storing strings over an alphabet:

Imagine you are implementing an auto-complete feature or search functionality or spell-checking or a phone-book implementation. Arrays are useless in these kinds of scenarios. You can use Tries instead. Tries are used for the rapid retrieval of keys from a big pile of strings. The trie data structure is frequently used when the input data is limited to lower case or upper case alphabets. Tries group similar words together and can be implemented in various real-life features.

Conclusion:

All and all, try and think about the various possibilities you may encounter while fetching and manipulating the data. If there is a data structure that comes to mind that may be more cost-efficient, ditch the array. You have a vast pool to choose from and you can even create your own data structure if the need arises.

Thank you for reading! This is Sarthak and until next time, peace out!