DEV Community

Awelle
Awelle

Posted on • Updated on

How I used a Hash Map to complete a task at my day job

I decided to share how I used a Hash Map to complete a task at work to hopefully help you begin to understand why they are important to know. Please keep in mind that I’m only going to be talking about the data structure known as a hash map. I didn't combine any of the popular algorithms with this data structure while I was working on this task, so I’m not highlighting any. Data structures are different from algorithms.

To illustrate the importance of using the right data structure when deciding how to do things, imagine wearing a shoe made of iron to play football or soccer. Do you think iron is the most appropriate material (data structure) to perform this activity? Likely not. No doubt, knowing and understanding data structures are important.

Prerequisites

To benefit the most from this article, you should already be interested in what data structures and algorithms are, and why there’s a lot of fuss about it, for example when preparing for technical interviews. You very likely have started cultivating this interest. Awesome!

What does understanding data structures mean?

Just like it's hard to define exactly what the quality called love is, it's a bit tricky to say exactly what ‘understanding’ data structures means. I can think of a couple of reasons why.

First, developers are at different stages in our journey, so there can’t be a definition that would work for everyone. Second, in my opinion, you cannot just understand data structures without coming to understand and appreciate certain things in computer science.

In my perspective, aligning your ideas and thinking more in line with a computer's function is an important step toward understanding data structures. A computer is an electrical device that does data processing and data storage, among other things. Computers may store data in a variety of different "containers." Even though you may not realize it yet, as a developer, you must be aware of all these "containers" in order to make wise choices when writing code to process data.

It, therefore, goes beyond simply writing code. Depending on the type of data you're programming the computer to process, you have to write code that is as clear and efficient as possible. In order to accomplish this, you must know about these data structures and their characteristics or features.

If at your current place of work, all you do is HTML and CSS, you might not have to make decisions about which data structure or data ‘container’ to use to store a certain kind of data or process data when writing code.

But be aware that at some point along the way, you'll have to start making that decision, so if you want to advance as a software engineer, you'll need to be aware of them and appreciate how crucial it is to be aware of them.

In my opinion, when you reach that conceptual stage, you have begun to ‘understand’ data structures and popular algorithms.

Okay, enough talk, let’s get to it!

The problem

I currently work at Pivo. Pivo builds fintech products that makes life easy for folks in the supply chain industry. I was recently directed to provide a weekly transaction report (with data sourced from our database) that clearly outlines the following data points which is a condensed report of credits and debits that happen on a weekly basis

  • Total transaction count per active customer (inflows and outflows)
  • The total amount of inflow per active Customer
  • The total amount of outflow per active customer

So assuming I got this from the database,

const result = [
  {

    amount: 12000,
    customer: ‘Nexus’,
     type:  CREDIT
    description: ‘commission’,

  },

{

    amount: 42000,
    customer: Cardillac,
    type:  DEBIT
    description: ‘Repairs - 2023 escalade,

  },
]
Enter fullscreen mode Exit fullscreen mode

After writing code to process the information above, I need to have the following information below outputed

Customer: Nexus, 
Total credit transactions amount: 12000, 
Total debit transactions amount: 0,
Total number of credit transactions: 1, 
Total number of debit transactions: 0



Customer: Cardillac,
Total credit transactions amount: 0, 
Total debit transactions amount: 42000,
Total number of credit transactions: 0, 
Total number of debit transactions: 1

Enter fullscreen mode Exit fullscreen mode

After thoroughly understanding the problem and thinking of edge cases, I came to the conclusion that utilizing the hashmap data structure would be the most effective way to handle this task.

The way a hashmap works is similar to how you would put different kinds of fruits in different bags as you buy them from the market. When you get home, if you wanted to know how many apples you bought, all you would need to do is count all the apples in the bag where only the apples are.
No need to sort all the fruits again and count. That would take more time. If you wrote code with that logic, it would take more time.

So here, I put each transaction of a particular customer in a separate ‘bag’. When it was time to calculate the total amount for a particular customer, for example, I just go to the ‘bag’ that contains only records for that customer and calculate it. I would not need to start worrying that some of them might be somewhere else because I know that all of them are in separate ‘bags’
I hope that makes sense.

Pseudocode

  1. Write a for loop to loop through the response array
  2. Initialize the hashmap. I code with Javascript mostly, so it looks like this
 const myMap = new Map()
Enter fullscreen mode Exit fullscreen mode
  1. One important thing I need to know when I get to each index in the for loop is whether that customer already exists on the map. If they do, I only need to update the transaction details accordingly. If it's a credit transaction, I’ll update the credit transaction amount and count. The opposite would be done if it's a debit transaction
  2. If they don’t exist, I can go ahead and set them up on the map

The time complexity (an estimate of how long it would take for the computer to do this operation) for this should ideally be an O(n). I only have to loop through the array once to process the data. The time complexity would grow as there are more records

Lets code this

 const transMap = new Map();         
 for (let i=0; i< result.length; i++){


   if(transMap.has( `${result[i].email }` ))
           {

             if ( result[i].transactionType === “CREDIT” ){

              const prevRec = transMap.get(`${result[i].email}`)

              const newRecord = prevRec.inflow+result[i].amount
              const newCount =  prevRec.inflowCount+1
              transMap.set( `${result[i].email }`, {inflow: newRecord, outflow: prevRec.outflow, customer: prevRec.customer, inflowCount: newCount, outflowCount: prevRec.outflowCount  })



             }else{

               const prevRec = transMap.get(`${result[i].email}`)
               const newRecord = prevRec.outflow+result[i].amount
               const newCount =  prevRec.outflowCount+1

               transMap.set( `${result[i].email }`, {inflow: prevRec.inflow, outflow:newRecord,  customer: prevRec.customer, inflowCount: prevRec.inflowCount, outflowCount: newCount })

             }


          }else{

            if (result[i].transactionType === “CREDIT”){

 transMap.set( `${result[i].email }`, {inflow: result[i].amount, customer:          result[i].recipientName, outflow: 0, inflowCount: 1, outflowCount: 0})

            }else{
              transMap.set( `${result[i].email }`, {inflow: 0, outflow: result[i].amount, customer: result[i].senderAccountName, inflowCount: 0, outflowCount: 1})
            }

          }


return transMap;

}

Enter fullscreen mode Exit fullscreen mode

Other things to think about (Test Cases)

The transaction amount can be six (6) figures. Yes, millions of dollars move through Pivo every day. I need to take care of this. This should be part of my test case.

When I’m adding the numbers, I need to make sure that they can add big numbers. I probably should initialize them as BigInt or something, but you get the point. Thinking about these edge cases would help refine the algorithm so that it becomes more reliable and accurate.

Conclusion

I hope that this article helps you in some way in your journey to understanding data structures and some of the algorithms related to them. I hope it helps to strengthen any existing mental models of this fascinating and important topic. Please feel free to leave a comment if you think there are better ways this could have been done or if you would like to ask me some questions. We can connect on LinkedIn too.

Cheers!

Top comments (0)