-
-
Notifications
You must be signed in to change notification settings - Fork 153
London | May-2025 | Reza Jahanimir | sprint1 #531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4571bd3
8d2d7bd
0b405b9
080e9cd
611cb8b
a1b686a
546f5f2
2a6ed4a
9b57eda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,22 @@ | ||
function dedupe() {} | ||
function dedupe(array) { | ||
// Validate input | ||
if (!Array.isArray(array)) return []; | ||
|
||
// set is a collection of unique values | ||
// It will automatically handle duplicates for us | ||
const seen = new Set(); // {} | ||
const result = []; | ||
|
||
// Iterate through the array | ||
// If the item is not in the set, add it to both the set and the result array | ||
for (const item of array) { | ||
if (!seen.has(item)) { | ||
seen.add(item); | ||
result.push(item); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
module.exports = dedupe; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,18 @@ | ||
function findMax(elements) { | ||
// Return -Infinity if the input is not an array | ||
if (!Array.isArray(elements)) { | ||
return -Infinity; | ||
} | ||
|
||
// Check if the input is an array and contains numeric values | ||
const numericValues = elements | ||
.filter((elements) => typeof elements === "number") | ||
|
||
// If the sorted list is empty, return null | ||
if (numericValues.length === 0) return -Infinity; | ||
|
||
// Return the largest numeric value found in the array | ||
return Math.max(...numericValues); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good code, but the comment has an inaccuracy. Please review the comment above this line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. your are right I am not working with a sorted array, i am simply filtering numeric values and getting the max |
||
} | ||
|
||
module.exports = findMax; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,23 @@ | ||
function sum(elements) { | ||
// Return -Infinity if the input is not an array | ||
if (!Array.isArray(elements)) { | ||
return null; | ||
} | ||
|
||
// Check if the input is an array and contains numeric values | ||
const numericValues = elements.filter( | ||
(elements) => typeof elements === "number" | ||
); | ||
|
||
// If there are no numeric values, return 0 | ||
if (numericValues.length === 0) return 0; | ||
|
||
// Return the sum of all numeric values in the array | ||
let sum = 0; | ||
for (let i = 0; i < numericValues.length; i++) { | ||
sum += numericValues[i]; | ||
} | ||
return sum; | ||
} | ||
|
||
module.exports = sum; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work! Can you explain why the if condition is necessary here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// if will compare the item with the item inside seen and as seen is a set it will not allow duplicate values
// when we loop through the array it will check if the item is already in the set or not
// if it is the if condition will be false and it will not add the item to the result array
// if it is not in the set it will add the item to the set and also to the result array
// so at the end we will have a result array with unique values only