[{"data":1,"prerenderedAt":410},["ShallowReactive",2],{"\u002Fblog\u002Fcode-log-day-1":3},{"id":4,"title":5,"author":6,"body":7,"date":395,"description":396,"draft":397,"extension":398,"image":399,"language":400,"meta":401,"navigation":92,"path":402,"seo":403,"stem":404,"tags":405,"__hash__":409},"blog\u002Fblog\u002FCode Log - Day 1.md","Code Log - Day 1","Caio Prado",{"type":8,"value":9,"toc":389},"minimark",[10,15,19,24,27,55,64,207,220,335,338,342,385],[11,12,14],"h2",{"id":13},"log","Log",[16,17,18],"p",{},"Today i decided to solve a LeetCode exercise and work on a side project open issues.",[20,21,23],"h3",{"id":22},"leetcode","LeetCode",[16,25,26],{},"Since there is a bit of time from the last time i solved a LeetCode exercise a chose easy one: Remove Element.\nIt requires a function that receive as input an array of integers numbers and a value to remove all occurrences of the value \"in-place\" and to return the number of the elements in the array that are not equal to the value:",[28,29,30],"blockquote",{},[16,31,32,33,37,38,41,42,44,45,48,49,51,52,54],{},"Change the array ",[34,35,36],"code",{},"nums"," such that the first ",[34,39,40],{},"k"," elements of ",[34,43,36],{}," contain the elements which are not equal to ",[34,46,47],{},"val",". The remaining elements of ",[34,50,36],{}," are not important as well as the size of ",[34,53,36],{},".",[16,56,57,58,60,61,63],{},"My first approach to achieve this was to put the elements that are equal at the end of the ",[34,59,36],{}," array. However, since the only thing that actually matters is the first ",[34,62,40],{}," elements, this solution ends up not being the best one.",[65,66,71],"pre",{"className":67,"code":68,"language":69,"meta":70,"style":70},"language-java shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","class Solution {\n    public int removeElement(int[] nums, int val) {\n\n        int val_occurrences = 0;\n        int nums_len = nums.length - 1;\n        for(int i=0, j=nums_len ; i\u003C=j ;i++){\n            if(nums[i] == val){\n                while(i!=j && nums[j] == val){\n                   val_occurrences++;\n                    j--;\n                }\n                int temp = nums[i];\n                nums[i] = nums[j];\n                nums[j] = temp;\n                val_occurrences++;\n                j--;\n\n            }\n        }\n        return nums.length - val_occurrences;\n    }\n}\n","java","",[34,72,73,81,87,94,100,106,112,118,124,130,136,142,148,154,160,166,172,177,183,189,195,201],{"__ignoreMap":70},[74,75,78],"span",{"class":76,"line":77},"line",1,[74,79,80],{},"class Solution {\n",[74,82,84],{"class":76,"line":83},2,[74,85,86],{},"    public int removeElement(int[] nums, int val) {\n",[74,88,90],{"class":76,"line":89},3,[74,91,93],{"emptyLinePlaceholder":92},true,"\n",[74,95,97],{"class":76,"line":96},4,[74,98,99],{},"        int val_occurrences = 0;\n",[74,101,103],{"class":76,"line":102},5,[74,104,105],{},"        int nums_len = nums.length - 1;\n",[74,107,109],{"class":76,"line":108},6,[74,110,111],{},"        for(int i=0, j=nums_len ; i\u003C=j ;i++){\n",[74,113,115],{"class":76,"line":114},7,[74,116,117],{},"            if(nums[i] == val){\n",[74,119,121],{"class":76,"line":120},8,[74,122,123],{},"                while(i!=j && nums[j] == val){\n",[74,125,127],{"class":76,"line":126},9,[74,128,129],{},"                   val_occurrences++;\n",[74,131,133],{"class":76,"line":132},10,[74,134,135],{},"                    j--;\n",[74,137,139],{"class":76,"line":138},11,[74,140,141],{},"                }\n",[74,143,145],{"class":76,"line":144},12,[74,146,147],{},"                int temp = nums[i];\n",[74,149,151],{"class":76,"line":150},13,[74,152,153],{},"                nums[i] = nums[j];\n",[74,155,157],{"class":76,"line":156},14,[74,158,159],{},"                nums[j] = temp;\n",[74,161,163],{"class":76,"line":162},15,[74,164,165],{},"                val_occurrences++;\n",[74,167,169],{"class":76,"line":168},16,[74,170,171],{},"                j--;\n",[74,173,175],{"class":76,"line":174},17,[74,176,93],{"emptyLinePlaceholder":92},[74,178,180],{"class":76,"line":179},18,[74,181,182],{},"            }\n",[74,184,186],{"class":76,"line":185},19,[74,187,188],{},"        }\n",[74,190,192],{"class":76,"line":191},20,[74,193,194],{},"        return nums.length - val_occurrences;\n",[74,196,198],{"class":76,"line":197},21,[74,199,200],{},"    }\n",[74,202,204],{"class":76,"line":203},22,[74,205,206],{},"}\n",[16,208,209,210,212,213,54],{},"An alternative and better approach, with 0.2 MB less memory usage compared with the first solution, only worry about placing the different values together at the beginning of ",[34,211,36],{}," by keeping an index\u002Fcount to these values, and can be found ",[214,215,219],"a",{"href":216,"rel":217},"https:\u002F\u002Fleetcode.com\u002Fproblems\u002Fremove-element\u002Fsolutions\u002F3670940\u002Fbest-100-c-java-python-beginner-friendly\u002F",[218],"nofollow","here",[65,221,223],{"className":67,"code":222,"language":69,"meta":70,"style":70},"class Solution {\n    public int removeElement(int[] nums, int val) {\n\n        int last_diff_val = 0;\n        for(int i = 0; i\u003Cnums.length; i++){\n            if(nums[i] != val){\n                nums[last_diff_val] = nums[i];\n                last_diff_val++;\n            }\n        }\n\n        \u002F*\n        \u002F\u002F this ways has a more clean read but uses more memory\n        for(int num_i : nums){\n            if(num_i != val){\n                nums[last_diff_val] = num_i;\n                last_diff_val++;\n            }\n        }\n        *\u002F\n\n        return last_diff_val;\n    }\n}\n",[34,224,225,229,233,237,242,247,252,257,262,266,270,274,279,284,289,294,299,303,307,311,316,320,325,330],{"__ignoreMap":70},[74,226,227],{"class":76,"line":77},[74,228,80],{},[74,230,231],{"class":76,"line":83},[74,232,86],{},[74,234,235],{"class":76,"line":89},[74,236,93],{"emptyLinePlaceholder":92},[74,238,239],{"class":76,"line":96},[74,240,241],{},"        int last_diff_val = 0;\n",[74,243,244],{"class":76,"line":102},[74,245,246],{},"        for(int i = 0; i\u003Cnums.length; i++){\n",[74,248,249],{"class":76,"line":108},[74,250,251],{},"            if(nums[i] != val){\n",[74,253,254],{"class":76,"line":114},[74,255,256],{},"                nums[last_diff_val] = nums[i];\n",[74,258,259],{"class":76,"line":120},[74,260,261],{},"                last_diff_val++;\n",[74,263,264],{"class":76,"line":126},[74,265,182],{},[74,267,268],{"class":76,"line":132},[74,269,188],{},[74,271,272],{"class":76,"line":138},[74,273,93],{"emptyLinePlaceholder":92},[74,275,276],{"class":76,"line":144},[74,277,278],{},"        \u002F*\n",[74,280,281],{"class":76,"line":150},[74,282,283],{},"        \u002F\u002F this ways has a more clean read but uses more memory\n",[74,285,286],{"class":76,"line":156},[74,287,288],{},"        for(int num_i : nums){\n",[74,290,291],{"class":76,"line":162},[74,292,293],{},"            if(num_i != val){\n",[74,295,296],{"class":76,"line":168},[74,297,298],{},"                nums[last_diff_val] = num_i;\n",[74,300,301],{"class":76,"line":174},[74,302,261],{},[74,304,305],{"class":76,"line":179},[74,306,182],{},[74,308,309],{"class":76,"line":185},[74,310,188],{},[74,312,313],{"class":76,"line":191},[74,314,315],{},"        *\u002F\n",[74,317,318],{"class":76,"line":197},[74,319,93],{"emptyLinePlaceholder":92},[74,321,322],{"class":76,"line":203},[74,323,324],{},"        return last_diff_val;\n",[74,326,328],{"class":76,"line":327},23,[74,329,200],{},[74,331,333],{"class":76,"line":332},24,[74,334,206],{},[336,337],"hr",{},[20,339,341],{"id":340},"side-project","Side Project",[16,343,344,345,349,350,353,354,357,358,361,362,365,366,369,370,373,374,365,377,380,381,384],{},"I decided to work on my {Second Brain](",[214,346,347],{"href":347,"rel":348},"https:\u002F\u002Fgithub.com\u002Fcaioopr\u002Fsecond_brain",[218],") project open issues. The issue i closed today was the \"Projects page\" one, where i had to finish the implementation of a page that display links to my projects description pages, just like the \"Posts\" page but for projects.\nIn this project, because i'm using Nuxt.js with the module Nuxt Content, it's really easy to add new pages and sub-pages, just like for the posts and post page it was enough to: add a folder ",[34,351,352],{},"projects"," at ",[34,355,356],{},"\u002Fpages\u002F"," and at ",[34,359,360],{},"\u002Fcontent","; create an ",[34,363,364],{},"index.md"," file at ",[34,367,368],{},"\u002Fcontent\u002Fprojects"," and define the component that will display the projects list with ",[34,371,372],{},"::component-name","; create a ",[34,375,376],{},"[...slug].vue",[34,378,379],{},"\u002Fpages\u002Fprojects"," and define the code responsible to render the single project page when accessing ",[34,382,383],{},"\u002Fprojects\u002Fproject_name"," in the browser.\nThe \"Projects page\" is already working. However , i'll probably be changing the design of the projects list item in the future, maybe add card style with a image preview.",[386,387,388],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":70,"searchDepth":83,"depth":83,"links":390},[391],{"id":13,"depth":83,"text":14,"children":392},[393,394],{"id":22,"depth":89,"text":23},{"id":340,"depth":89,"text":341},"2025-01-07","Solving LeetCode's Remove Element in Java and finishing the Projects page of my Second Brain side project.",false,"md",null,"en",{},"\u002Fblog\u002Fcode-log-day-1",{"title":5,"description":396},"blog\u002FCode Log - Day 1",[406,407,22,408],"challenge","daily-coding","nuxt.js","NasmeerxlORsQ-h_qUdYA227vIRazF1J7GjyuoXB06g",1785357636473]