My LeetCode Journey Day 1
Chinaza Adimoha
Senior Frontend Developer | Typescript | ReactJS Developer | Next JS && SSR | Fintech | Banking(Core Banking) | HealthTech (EHR) | Real Estate Tech | Creating Responsive High Performance Software Products |
One of my New Year's resolutions for 2024, was to sharpen my problem-solving skills. A friend of mine recommended a very impressive tool that categorizes leet code problems, from beginner level, all the way to advanced level. It also groups different leet code problems into their respective data structures. I started off my journey yesterday, solving at least two problems every day using NeetCode.
The first problem I solved was the famous "Identifying a Duplicate". I had to think about the problem carefully and come up with an optimal solution (0n)
here's my approach...
领英推荐
I hope you find this insightful. I dropped comments to explain my thought process.
Thank you for reading
// Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
// Example 1:
// Input: nums = [1,2,3,1]
// Output: true
// Example 2:
// Input: nums = [1,2,3,4]
// Output: false
// Example 3:
// Input: nums = [1,1,1,3,3,4,3,2,4,2]
// Output: true
var containsDuplicate = function(nums) {
//create a buffer to store intergers first of
//create variable to identify unique value
const set = new Set();
//I want to loop through the array to check for the duplicate value
for(let num of nums) {
if(set.has(num)) return true
//here's where we add to the buffer
set.add(num)
}
//return false if nums is not a duplicate
return false
};
//second solution using the size of the set
var containsDuplicate = function(nums) {
// create an unordered set then check if the size of the set is equal to the size of the array
return new Set(nums).size !== nums.length;
};
Fintech | Product Design & Operations Management | Driving User-Centric Innovation | Women in Tech Inclusion Advocate | AI Enthusiastic
1 年Nice one ????
Software engineer | Digital Health Specialist | Java | React | Multimodal Application development | Containerization and Microservices | Relational Databases | Continuous Integration & Deployment
1 年Haha... Let's go