The intersection of Two Lines

The intersection of two lines posted in leetcodes did not seem that complex. Based on the title my initial thought was to find an intersecting x and y-axis, fortunately, I was so wrong. It was similar to the most frequent number problem. The question in Leetcode states “Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.” Here are a few examples below.

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
Explanation: [9,4] is also accepted.

Attacking the problem.

I started with sorting the two given arrays. The reasoning behind this was based on the question. I was given the ability to return it in any order. Therefore I decided to sort my numbers and find the element that appears x amount of times. Here’s a closer look at my sorting technique below.

var intersect = function(nums1, nums2) {

nums1.sort((a,b) => a - b)
nums2.sort((a, b) => a - b)


};

Right after sorting the given array I created two variables and set them to zero. Both variables would give me the leverage to start at a particular index in the array and fit into my while loop scheme. Furthermore, I created an empty array to return the elements.

var intersect = function(nums1, nums2) {

nums1.sort((a,b) => a - b)
nums2.sort((a, b) => a - b)

let i = 0
let j = 0
let result = []


};

I created a while loop that evaluates to true and goes through both arrays. In addition to the loop, I created a conditional statement that compares both arrays to one another. Once that condition is met I continued to increment through the array. Here’s a better look below.

var intersect = function(nums1, nums2) {

nums1.sort((a,b) => a - b)
nums2.sort((a, b) => a - b)

let i = 0
let j = 0
let result = []

while(i < nums1.length && j < nums2.length){
if(nums1[i] < nums2[j]){
i++
} else if (nums1[i] < nums2[j]){
j++
}
}

};

At the end of my condition, I added an else statement that grabs the frequent element and pushed it to the first array. Furthermore, I continued to increment through both arrays till all elements were found and returned with my empty array.

var intersect = function(nums1, nums2) {

nums1.sort((a,b) => a - b)
nums2.sort((a, b) => a - b)

let i = 0
let j = 0
let result = []

while(i < nums1.length && j < nums2.length){
if(nums1[i] < nums2[j]){
i++
} else if (nums1[i] < nums2[j]){
j++
} else {
result.push(nums1[i])
i++
j++
}
}

return result

};

Thanks for reading.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Working with the Full Stack in Javascript

How bad is e2e test performance really? (rtl vs cypress vs playwright vs testcafe)

Footer, Offer and Contact.

Day 7: States in React

Setting up a NextJS env with Yarn 2

Fun Things You Can Build to Learn Your Next Framework

Angular Basics-4

The Difference Between created() and mounted() in Vue.js

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Woodelin florveus

Woodelin florveus

More from Medium

All the steps of compilation in C language

JavaScript Array in 5 minute reading

Meta Programming in Ruby

Animating with AnimeJS