Learn
Exam

Finding Common Elements in Two Arrays

Difficulty
STARTER

Problem

Given two arrays numbers1 and numbers2 consisting of integer elements, write a function solution that finds the common elements between numbers1 and numbers2, puts them in an array, sorts them in ascending order, and returns the result.


Constraints

  • Each element in numbers1 and numbers2 is an integer with an absolute value not exceeding 500.
  • Each array has unique elements, meaning cases like [1, 1, 2, 3] are not given.
  • Both arrays are not empty, and their lengths do not exceed 100.

Test Case

Test Case Number numbers1 numbers2 return
1 [1] [1] [1]
2 [1, 2, 3] [1, 2, 3, 4] [1, 2, 3]
3 [-204, -111, 0, 1, 450] [1, -204, 111, 0] [-204, 0, 1]

Explanation for Test Case 1

  • The common element between numbers1 and numbers2 is 1.

Explanation for Test Case 2

  • The common elements between numbers1 and numbers2 are 1, 2, 3.

Explanation for Test Case 3

  • The common elements between numbers1 and numbers2 are -204, 0, 1.