Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the intersection of two arrays by JavaScript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to achieve the intersection of two arrays in JavaScript". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve the intersection of two arrays in JavaScript" can help you solve the problem.

The intersection of two arrays I

Given two arrays nums1 and nums2, return their intersection. Each element in the output must be unique. We can ignore the order of the output results.

Example 1:

Input: nums1 = [1pm 2je 2je 1], nums2 = [2je 2]

Output: [2]

Example 2:

Input: nums1 = [4JEI 9JEI 5], nums2 = [9Jet 4JEI 8JE4]

Output: [9pd4]

Explanation: [4pd9] is also acceptable.

Note:

1 aMub) let res = [] let key1 = 0, key2 = 0, index = 0 while / keep pushing while (key1) without two pointers reaching the boundary.

< nums1.length && key2 < nums2.length){//判断nums1[key1]与nums2[key2]的大小,分出大于小于等于三种情况if(nums1[key1] < nums2[key2]) key1++else if(nums1[key1] >

Nums2 [key2]) key2++else {res [index++] = Nums1 [key1] key1++key2++}} return res}; that's all for "how JavaScript achieves the intersection of two arrays". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report