GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/problems/intersection-of-two-arrays at master · ecgan/leetcode

Expand file tree

/

intersection-of-two-arrays

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

intersection-of-two-arrays

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
README.md
README.md
 
 
solution.js
solution.js
 
 
solution.test.js
solution.test.js
 
 

README.md

Outline

Intersection of Two Arrays

Link to LeetCode page

Difficulty: Easy

Topics: Hash table, two pointers, binary search, sort, set.

Solution Explanation

We convert the arrays into two Set objects - short set and long set. This would remove duplicate values in each array.

Then we iterate through the short set and check if the value exists in the long set. Since it is a set object, the check performs in constant O(1) time complexity.

Complexity Analysis

Assume m is the length of nums1 and n is the length of nums2.

Time complexity: O(m + n) because we go through every elements in both arrays once during the set creation.

Space complexity: O(m + n) because in worst case scenario we create two sets of length m and n when both arrays contain totally unique values.

Tests

In the problem description on LeetCode page, it is noted that:

  • Each element in the result must be unique.
  • The result can be in any order.

Because of this, the assertion in our tests has to be in the following format:

  expect(result).toHaveLength(2)
  expect(result).toContain(9)
  expect(result).toContain(4)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.