Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the result variable to zero.
DSA Go
result := [1]Attempts:
3 left
💡 Hint
Common Mistakes
Initializing result to 1 or -1 causes wrong output.
Using nil causes compilation error.
✗ Incorrect
We start with zero because XOR with zero leaves the number unchanged.
2fill in blank
mediumComplete the code to XOR each element with the result.
DSA Go
for _, num := range nums { result = result [1] num }
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or & instead of ^ changes the logic.
Using | does not cancel duplicates.
✗ Incorrect
XOR operator (^) is used to find the unique element by cancelling duplicates.
3fill in blank
hardFix the error in the function signature to accept a slice of integers.
DSA Go
func findUnique([1] []int) int { result := 0 for _, num := range [1] { result = result ^ num } return result }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names causes compilation errors.
Using singular 'num' as parameter name is confusing.
✗ Incorrect
The parameter name must be consistent; 'nums' is used in the function body.
4fill in blank
hardFill both blanks to complete the main function that calls findUnique and prints the result.
DSA Go
func main() {
nums := []int{2, 3, 5, 4, 5, 3, 4}
unique := [1](nums)
fmt.[2](unique)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using Print does not add newline.
Calling undefined function causes error.
✗ Incorrect
Call findUnique to get the unique number and use fmt.Println to print it with a newline.
5fill in blank
hardFill all three blanks to import the required package, define the function, and print the unique element.
DSA Go
package main import [1] func [2](nums []int) int { result := 0 for _, num := range nums { result = result ^ num } return result } func main() { nums := []int{1, 2, 1, 3, 2} unique := [3](nums) fmt.Println(unique) }
Attempts:
3 left
💡 Hint
Common Mistakes
Importing wrong package causes error.
Mismatched function names cause compilation errors.
✗ Incorrect
Import fmt package, define findUnique function, and call it in main to print the unique element.
