Day 8 (30 Days of Code)

Day 8 (30 Days of Code)

7 July 2023

Today was yet another productive day learning React. I learned so many React concepts with a simple ToDo app. Also I solved 2 Leetcode questions as my daily routine.

What did I learn?

  • Props

  • Mapping components

  • Hooks

  • Event Handling

  • React forms

  • Complex state management

  • SpreadOperator

  • Component Tree

These are the thing that I learned while building a simple and easy ToDo list app using React.

I made one ToDo app before using simple EJS and NodeJS in which I also added a database. But making it using React was a whole other experience. I will also connect this ToDo app with the database as well shortly.

todo app 1

Adding a new item

todo app 2

Deleting an item

We can do that simply by clicking on the name of the item and using a filter method.

todo app 3

I'll be providing the GitHub repository for this in future articles after I successfully connected it to a backend server and a database. Also I'll be trying to host it as well.

DSA for the day

My approach:

I could've done this question through a simple while loop. However, I wanted to try doing this question through Recursion and after a little struggle I was able to come up with a recursive solution.

My solution:

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head==NULL){
            return NULL;
        }
        if(head->next==NULL){
            return head;
        }

        ListNode* reverseHead = reverseList(head->next);

        head->next->next = head;
        head->next = NULL;

        return reverseHead;
    }
};

My approach:

My very first solution would have been using 2 loops but now that I am little experienced I first came with the nlogn in which my space complexity was constant. But later I came with the solution of linear time complexity, however, the space complexity came out to be linear as well.

  1. Using Sorting:

    Time complexity : O(nlogn)

    Space complexity : O(1)

class Solution {

public:
    bool containsDuplicate(vector<int>& nums) {
        int n = sizeof(nums)/sizeof(nums[0]);
        sort(nums.begin(), nums.end());

        for(int i=1;i<nums.size();i++){
            if(nums[i-1]==nums[i]){
                return true;
            }
        }
        return false;
    }
};
  1. Using a Hashmap:

    Time complexity : O(n)

    Space complexity : O(n)

     class Solution {
    
     public:
         bool containsDuplicate(vector<int>& nums) {
             unordered_map<int,int> map;
             vector<int> output;
    
             for(int i=0;i<nums.size();i++){
                 if(map.count(nums[i])>0){
                     map[nums[i]]++;
                     continue;
                 }
                 map[nums[i]] = 1;
                 output.push_back(nums[i]);
             }
    
             for(int i=0;i<output.size();i++){
                 if(map[output[i]]>1){
                     return true;
                 }
             }
             return false;
         }
     };