Skip to content

Commit

Permalink
solution(javascript): Problems 26 and 344
Browse files Browse the repository at this point in the history
Problems:
26. Remove Duplicates from Sorted Array
344. Reverse String
  • Loading branch information
godkingjay authored Oct 11, 2023
2 parents e8c49b7 + f9a0d3b commit 18fedc0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Easy/26. Remove Duplicates from Sorted Array/Solution.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Solution # 1
const removeDuplicates = function (nums) {
let uniqueCount = 1; // Initialize the count of unique elements.

Expand All @@ -11,3 +12,15 @@ const removeDuplicates = function (nums) {

return uniqueCount;
};

// Solution # 2
function removeDuplicates(nums) {
// Use filter to create a new array with unique elements
const uniqueArray = nums.filter((value, index) => nums.indexOf(value) === index);

// Copy the unique elements back to the original array
for (let i = 0; i < uniqueArray.length; i++) {
nums[i] = uniqueArray[i];
}
return uniqueArray.length;
}
4 changes: 4 additions & 0 deletions Easy/344. Reverse String/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var reverseString = function (s) {
s.reverse();
return s;
};

0 comments on commit 18fedc0

Please sign in to comment.