DEV Community

Matsu
Matsu

Posted on • Updated on

Leetcode Challenge - Two Sum (#1) | Hashmap & hasOwnProperty method

Diving into the LeetCode world has been a wild ride for me! I tried everything that I could, but I always got stuck at some test.

Although I learned an important thing: we need to keep studying, no matter when. Sometimes we get tired of the job, of studying, and everything, but it's important to keep up the study and exercise your brain somehow.

During my first try, I tried using everything I knew, but I didn't pass all the tests. I always messed up at some point.

Studying the possible solutions, I came across with "Hashmap" and when I tried to apply it through ChatGPT, he gave me the answer. I didn't mean it.

Well, I felt like a failure when I came across the solution, but at least I wanted to really understand what's going on, and I'll tell everything that I learned in this LeetCode problem.


Two important lessons:
1. Understanding Hash maps and their application in JavaScript.

Hash map basically it will always follow the pattern of key-value. In JavaScript, hash maps are represented as objects, offering a quick and efficient way to organize and access data. Think of them as labeled boxes where each item (data) has a unique key. Retrieving, adding, and managing data becomes faster and more organized, making hash maps a powerful tool for various programming tasks.

For example:

// Creating a simple hash map in JavaScript
let userPreferences = {
  'theme': 'dark',
  'language': 'javascript',
  'fontSize': 'medium'
};

// Accessing data using keys
console.log(userPreferences['theme']);  // Output: dark

// Adding new data
userPreferences['background'] = 'pattern';

// Hash maps make data retrieval fast and easy
console.log(userPreferences['background']);  // Output: pattern

Enter fullscreen mode Exit fullscreen mode

2. Exploring the hasOwnProperty() method in JavaScript.
The hasOwnProperty() method in JavaScript is like a safety check for object properties. It allows you to confirm if an object possesses a specific property before attempting to use it. This helps prevent error and ensures a smoother, more reliable execution of your code by verifying the existence of properties within an object.
For example:

// Creating a nerdy character object
let wizardCharacter = {
  'name': 'Gandalf',
  'powerLevel': 9000,
  'staff': 'The Grey One'
};

// Checking if a magical attribute exists using hasOwnProperty()
console.log(wizardCharacter.hasOwnProperty('name'));  // Output: true
console.log(wizardCharacter.hasOwnProperty('petDragon')); // Output: false

// Safely wielding magical attributes with hasOwnProperty()
if (wizardCharacter.hasOwnProperty('powerLevel')) {
  console.log(`${wizardCharacter['name']}'s power level: ${wizardCharacter['powerLevel']}`);
} else {
  console.log('Power level not found. Did someone steal the spellbook?');
}
Enter fullscreen mode Exit fullscreen mode

If you're interested in reviewing my coding solution, you can find the code on my GitHub repository here. I welcome any feedback or suggestions you may have.

Console You Later!

Top comments (0)