-
-
Notifications
You must be signed in to change notification settings - Fork 713
Add solution for Challenge 19 by iamsurajmandal #716
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
Changes from all commits
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||
| package main | ||||||
|
|
||||||
| import ( | ||||||
| "fmt" | ||||||
| "math" | ||||||
| ) | ||||||
|
|
||||||
| func main() { | ||||||
| // Example slice for testing | ||||||
| numbers := []int{3, 1, 4, 1, 5, 9, 2, 6} | ||||||
|
|
||||||
| // Test FindMax | ||||||
| max := FindMax(numbers) | ||||||
| fmt.Printf("Maximum value: %d\n", max) | ||||||
|
|
||||||
| // Test RemoveDuplicates | ||||||
| unique := RemoveDuplicates(numbers) | ||||||
| fmt.Printf("After removing duplicates: %v\n", unique) | ||||||
|
|
||||||
| // Test ReverseSlice | ||||||
| reversed := ReverseSlice(numbers) | ||||||
| fmt.Printf("Reversed: %v\n", reversed) | ||||||
|
|
||||||
| // Test FilterEven | ||||||
| evenOnly := FilterEven(numbers) | ||||||
| fmt.Printf("Even numbers only: %v\n", evenOnly) | ||||||
| } | ||||||
|
|
||||||
| // FindMax returns the maximum value in a slice of integers. | ||||||
| // If the slice is empty, it returns 0. | ||||||
| func FindMax(numbers []int) int { | ||||||
| if (len(numbers) < 1) { | ||||||
| return 0 | ||||||
| } | ||||||
| max := int(math.Inf(-1)) | ||||||
| for i := 0; i < len(numbers); i++ { | ||||||
| if (numbers[i] > max) { | ||||||
| max = numbers[i] | ||||||
| } | ||||||
| } | ||||||
| return max | ||||||
| } | ||||||
|
|
||||||
| // RemoveDuplicates returns a new slice with duplicate values removed, | ||||||
| // preserving the original order of elements. | ||||||
| func RemoveDuplicates(numbers []int) []int { | ||||||
| // TODO: Implement this function | ||||||
|
Contributor
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. Remove TODO comment. The function is fully implemented—remove the placeholder TODO comment. - // TODO: Implement this function
hs := map[int]bool{}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| hs := map[int]bool{} | ||||||
| result := []int{} | ||||||
| for _, value := range numbers { | ||||||
| _, exists := hs[value] | ||||||
| if !exists { | ||||||
| hs[value] = true | ||||||
| result = append(result, value) | ||||||
| } | ||||||
| } | ||||||
| return result | ||||||
| } | ||||||
|
|
||||||
| // ReverseSlice returns a new slice with elements in reverse order. | ||||||
| func ReverseSlice(slice []int) []int { | ||||||
| result := []int{} | ||||||
| // TODO: Implement this function | ||||||
|
Contributor
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. Remove TODO comment. The function is fully implemented—remove the placeholder TODO comment. - // TODO: Implement this function
for i := len(slice) - 1; i >= 0 ; i-- {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| for i := len(slice) - 1; i >= 0 ; i-- { | ||||||
| result = append(result, slice[i]) | ||||||
| } | ||||||
| return result | ||||||
| } | ||||||
|
|
||||||
| // FilterEven returns a new slice containing only the even numbers | ||||||
| // from the original slice. | ||||||
| func FilterEven(numbers []int) []int { | ||||||
| result := []int{} | ||||||
| for _, value := range numbers{ | ||||||
| if (value % 2 == 0 ) { | ||||||
| result = append(result, value) | ||||||
| } | ||||||
| } | ||||||
| return result | ||||||
| } | ||||||
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.
Fix: Avoid converting math.Inf to int.
Converting
math.Inf(-1)(negative infinity) tointis problematic—the behavior is implementation-defined and can produce unexpected results. Initializemaxwith the first element after confirming the slice is non-empty, or usemath.MinInt.Apply this diff to use a clearer initialization:
func FindMax(numbers []int) int { - if (len(numbers) < 1) { + if len(numbers) == 0 { return 0 } - max := int(math.Inf(-1)) - for i := 0; i < len(numbers); i++ { - if (numbers[i] > max) { - max = numbers[i] - } - } + max := numbers[0] + for _, num := range numbers { + if num > max { + max = num + } + } return max }🤖 Prompt for AI Agents