Bird
Raised Fist0
NumPydata~20 mins

np.setdiff1d() for difference in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Set Difference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A[1 2 3 4 5 6]
B[3 4 6]
C[1 2 5]
D[6]
Attempts:
2 left
💡 Hint
np.setdiff1d returns elements in the first array that are NOT in the second array.
❓ data_output
intermediate
1: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))
A3
B2
C4
D5
Attempts:
2 left
💡 Hint
Count elements in arr1 that are not in arr2.
🔧 Debug
advanced
1: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)
ASyntaxError
B[1]
CTypeError: unhashable type: 'list'
DNo error, output: [1]
Attempts:
2 left
💡 Hint
np.setdiff1d can accept lists as input because it converts them internally to arrays.
❓ Predict Output
advanced
2: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)
A[1 3 5]
B[1 2 3 4 5]
C[1 3 4 5]
D[2 4]
Attempts:
2 left
💡 Hint
np.setdiff1d returns unique sorted values from arr1 not in arr2.
🚀 Application
expert
2: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)
Anp.union1d(survey1, survey2)
Bnp.setdiff1d(survey1, survey2)
Cnp.intersect1d(survey1, survey2)
Dnp.setdiff1d(survey2, survey1)
Attempts:
2 left
💡 Hint
We want users in survey1 but not in survey2.

Practice

(1/5)
1.

What does the function np.setdiff1d(arr1, arr2) do?

easy
A. Finds elements in arr1 that are not in arr2
B. Finds elements common to both arr1 and arr2
C. Combines arr1 and arr2 into one array
D. Sorts arr1 in descending order

Solution

  1. Step 1: Understand the function purpose

    np.setdiff1d(arr1, arr2) returns elements unique to arr1 that are not found in arr2.
  2. Step 2: Compare with options

    Finds elements in arr1 that are not in arr2 matches this behavior exactly, while others describe different operations.
  3. Final Answer:

    Finds elements in arr1 that are not in arr2 -> Option A
  4. Quick Check:

    Unique elements in arr1 = D [OK]
Hint: Remember: setdiff1d finds what is only in first array [OK]
Common Mistakes:
  • Confusing setdiff1d with intersection
  • Thinking it merges arrays
  • Assuming it sorts in descending order
2.

Which of the following is the correct syntax to find elements in a not in b using np.setdiff1d()?

a = np.array([1, 2, 3])
b = np.array([2, 3, 4])
easy
A. np.setdiff1d(a + b)
B. np.setdiff1d(b, a)
C. np.setdiff1d(a)
D. np.setdiff1d(a, b)

Solution

  1. Step 1: Identify correct parameter order

    The first argument is the array to find unique elements from; the second is the array to exclude elements from.
  2. Step 2: Match with options

    To find elements in a not in b, use np.setdiff1d(a, b), which is np.setdiff1d(a, b).
  3. Final Answer:

    np.setdiff1d(a, b) -> Option D
  4. Quick Check:

    First array minus second array = A [OK]
Hint: First argument is the array to subtract from [OK]
Common Mistakes:
  • Swapping the order of arrays
  • Passing only one array
  • Trying to add arrays inside setdiff1d
3.

What is the output of the following code?

import numpy as np
x = np.array([5, 3, 9, 1])
y = np.array([3, 7, 1])
result = np.setdiff1d(x, y)
print(result)
medium
A. [1 3 5 7 9]
B. [5 9]
C. [3 7]
D. [1 5 9]

Solution

  1. Step 1: Identify elements in x not in y

    Elements in x are [5, 3, 9, 1]. Elements in y are [3, 7, 1]. The elements in x but not in y are 5 and 9.
  2. Step 2: Check output format

    np.setdiff1d returns a sorted array without duplicates, so output is [5 9].
  3. Final Answer:

    [5 9] -> Option B
  4. Quick Check:

    Unique elements in x = A [OK]
Hint: Output is sorted unique elements from first array only [OK]
Common Mistakes:
  • Including elements from second array
  • Not sorting output
  • Confusing with intersection output
4.

Find the error in this code snippet:

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([2, 3, 4])
result = np.setdiff1d(arr1 arr2)
print(result)
medium
A. Missing comma between arguments in setdiff1d
B. Arrays must be lists, not numpy arrays
C. Function name is misspelled
D. print statement syntax is incorrect

Solution

  1. Step 1: Check function call syntax

    The call np.setdiff1d(arr1 arr2) is missing a comma between arr1 and arr2.
  2. Step 2: Verify other parts

    Arrays are correctly numpy arrays, function name is correct, and print syntax is valid in Python 3.
  3. Final Answer:

    Missing comma between arguments in setdiff1d -> Option A
  4. Quick Check:

    Comma separates arguments = C [OK]
Hint: Check commas between function arguments carefully [OK]
Common Mistakes:
  • Forgetting commas between parameters
  • Thinking numpy arrays are invalid inputs
  • Assuming print needs parentheses in Python 3
5.

You have two arrays representing IDs of users who visited two different websites:

site1 = np.array([101, 102, 103, 104, 105])
site2 = np.array([103, 104, 106])

How can you find the IDs of users who visited only the first site?

hard
A. np.setdiff1d(site2, site1)
B. np.intersect1d(site1, site2)
C. np.setdiff1d(site1, site2)
D. np.union1d(site1, site2)

Solution

  1. Step 1: Understand the problem

    We want users who visited site1 but not site2.
  2. Step 2: Apply np.setdiff1d

    Using np.setdiff1d(site1, site2) returns elements in site1 not in site2.
  3. Step 3: Check other options

    np.setdiff1d(site2, site1) reverses the order, giving users only in site2. Options C and D find common or combined users, not unique to site1.
  4. Final Answer:

    np.setdiff1d(site1, site2) -> Option C
  5. Quick Check:

    Unique to first array = B [OK]
Hint: Use setdiff1d with first array as main set [OK]
Common Mistakes:
  • Swapping arrays order
  • Using intersection instead of difference
  • Using union which combines all