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
Recall & Review
beginner
What does np.intersect1d() do?
It finds the common elements between two arrays and returns them as a sorted array without duplicates.
Click to reveal answer
beginner
How does np.intersect1d() handle duplicates in input arrays?
It removes duplicates and returns only unique common elements in the output.
Click to reveal answer
beginner
Example: What is the output of np.intersect1d([1, 2, 3], [2, 3, 4])?
The output is array([2, 3]) because 2 and 3 are common in both arrays.
Click to reveal answer
intermediate
Can np.intersect1d() be used with arrays of different data types?
Yes, but the arrays should be compatible types so that comparison is possible.
Click to reveal answer
intermediate
What is the difference between np.intersect1d() and Python's set intersection?
np.intersect1d() works on numpy arrays and returns a sorted array without duplicates, while set intersection works on Python sets and returns an unordered set.
Click to reveal answer
What does np.intersect1d() return?
ACommon elements sorted without duplicates
BAll elements from the first array
CAll elements from the second array
DConcatenation of both arrays
✗ Incorrect
np.intersect1d() returns the sorted unique elements common to both arrays.
If a = [1, 2, 2, 3] and b = [2, 2, 4], what is np.intersect1d(a, b)?
A[2, 2]
B[2]
C[1, 2, 3, 4]
D[1, 3, 4]
✗ Incorrect
Duplicates are removed, so only one 2 is returned.
Which of these is true about np.intersect1d()?
AReturns unsorted array
BReturns only elements from first array
CReturns sorted array
DReturns duplicates
✗ Incorrect
The output is always sorted and unique.
Can np.intersect1d() be used with string arrays?
AOnly with boolean arrays
BNo, only numeric arrays
COnly with integer arrays
DYes, it works with any comparable data type
✗ Incorrect
It works with any arrays where elements can be compared, including strings.
What happens if there are no common elements between arrays?
AReturns an empty array
BReturns the first array
CReturns the second array
DRaises an error
✗ Incorrect
If no elements match, the result is an empty array.
Explain how np.intersect1d() works and give a simple example.
Think about finding shared items in two lists.
You got /3 concepts.
Describe the difference between np.intersect1d() and Python set intersection.
Consider data types and output format.
You got /3 concepts.
Practice
(1/5)
1. What does the function np.intersect1d() do in NumPy?
easy
A. Finds common elements between two arrays
B. Combines two arrays into one
C. Sorts an array in descending order
D. Removes duplicates from an array
Solution
Step 1: Understand the function purpose
np.intersect1d() is designed to find elements that appear in both input arrays.
Step 2: Compare with other options
Options A, B, and D describe different functions: sorting descending, combining arrays, and removing duplicates, which are not the purpose of np.intersect1d().
Final Answer:
Finds common elements between two arrays -> Option A
Quick Check:
Intersection = common elements [OK]
Hint: Think 'intersection' means shared items [OK]
Common Mistakes:
Confusing intersection with concatenation
Thinking it sorts descending
Mixing with duplicate removal
2. Which of the following is the correct syntax to find the intersection of arrays a and b using NumPy?
easy
A. np.intersect(a, b)
B. np.intersection(a, b)
C. np.intersect1d(a, b)
D. np.intersect1d([a, b])
Solution
Step 1: Recall the correct function name and parameters
The correct function is np.intersect1d() and it takes two arrays as separate arguments.
Step 2: Check each option
np.intersect1d(a, b) uses the correct function and syntax. Options B and C use incorrect function names. np.intersect1d([a, b]) incorrectly passes a list of arrays instead of two separate arguments.
Final Answer:
np.intersect1d(a, b) -> Option C
Quick Check:
Correct function and parameters [OK]
Hint: Use exact function name and separate arrays as arguments [OK]
Common Mistakes:
Using wrong function names
Passing arrays inside a list
Missing one argument
3. What is the output of the following code?
import numpy as np
x = np.array([3, 1, 4, 1, 5])
y = np.array([5, 9, 2, 6, 5])
print(np.intersect1d(x, y))
medium
A. [1 5]
B. [3 5]
C. [1 5 9]
D. [5]
Solution
Step 1: Identify unique elements in both arrays
Array x has elements {1, 3, 4, 5} (1 appears twice but counted once). Array y has elements {2, 5, 6, 9} (5 appears twice but counted once).
Step 2: Find common elements
The only common element between x and y is 5.
Final Answer:
[5] -> Option D
Quick Check:
Common elements = [5] [OK]
Hint: Look for numbers appearing in both arrays [OK]
Common Mistakes:
Including duplicates
Adding elements not in both arrays
Confusing order of output
4. The following code throws an error. What is the problem?
import numpy as np
arr1 = [1, 2, 3]
arr2 = [2, 3, 4]
result = np.intersect1d(arr1 arr2)
print(result)
medium
A. Missing comma between arguments in np.intersect1d()
B. Arrays must be NumPy arrays, not lists
C. np.intersect1d() does not accept two arguments
D. print() function is used incorrectly
Solution
Step 1: Check the function call syntax
The call np.intersect1d(arr1 arr2) is missing a comma between the two arguments.
Step 2: Verify other parts
Passing lists is allowed because NumPy converts them internally. The function accepts two arguments. The print statement is correct.
Final Answer:
Missing comma between arguments in np.intersect1d() -> Option A
Quick Check:
Arguments must be separated by commas [OK]
Hint: Check commas between function arguments [OK]
Common Mistakes:
Forgetting commas
Thinking lists are invalid inputs
Misreading error source
5. You have two arrays representing product IDs sold in two stores: