Challenge - 5 Problems
Set Difference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.setdiff1d() with simple arrays
What is the output of the following code snippet?
NumPy
import numpy as np arr1 = np.array([1, 2, 3, 4, 5]) arr2 = np.array([3, 4, 6]) result = np.setdiff1d(arr1, arr2) print(result)
Attempts:
2 left
💡 Hint
np.setdiff1d returns elements in the first array that are NOT in the second array.
✗ Incorrect
np.setdiff1d(arr1, arr2) returns elements from arr1 that are not present in arr2. Here, 1, 2, and 5 are in arr1 but not in arr2.
❓ data_output
intermediate1:30remaining
Number of elements in difference array
How many elements are in the resulting array after running this code?
NumPy
import numpy as np arr1 = np.array([10, 20, 30, 40, 50]) arr2 = np.array([20, 40, 60]) result = np.setdiff1d(arr1, arr2) print(len(result))
Attempts:
2 left
💡 Hint
Count elements in arr1 that are not in arr2.
✗ Incorrect
Elements 10, 30, and 50 are in arr1 but not in arr2, so the length is 3.
🔧 Debug
advanced1:30remaining
Identify the error in using np.setdiff1d with lists
What error will this code produce?
NumPy
import numpy as np list1 = [1, 2, 3] list2 = [2, 3, 4] result = np.setdiff1d(list1, list2) print(result)
Attempts:
2 left
💡 Hint
np.setdiff1d can accept lists as input because it converts them internally to arrays.
✗ Incorrect
np.setdiff1d accepts lists and converts them to arrays internally, so no error occurs and output is [1].
❓ Predict Output
advanced2:00remaining
Output of np.setdiff1d with repeated elements
What is the output of this code?
NumPy
import numpy as np arr1 = np.array([1, 2, 2, 3, 4, 4, 5]) arr2 = np.array([2, 4]) result = np.setdiff1d(arr1, arr2) print(result)
Attempts:
2 left
💡 Hint
np.setdiff1d returns unique sorted values from arr1 not in arr2.
✗ Incorrect
Repeated elements are ignored; only unique values from arr1 not in arr2 remain: 1, 3, 5.
🚀 Application
expert2:30remaining
Using np.setdiff1d to find missing data entries
Given two arrays representing IDs of users who completed two different surveys, which option correctly finds users who completed the first survey but NOT the second?
NumPy
import numpy as np survey1 = np.array([101, 102, 103, 104, 105]) survey2 = np.array([102, 104, 106]) missing_users = ??? print(missing_users)
Attempts:
2 left
💡 Hint
We want users in survey1 but not in survey2.
✗ Incorrect
np.setdiff1d(survey1, survey2) returns elements in survey1 missing from survey2, which is the correct way to find users who completed only the first survey.