当前位置:网站首页>JS merge multiple string arrays to maintain the original order and remove duplicates
JS merge multiple string arrays to maintain the original order and remove duplicates
2022-06-13 02:52:00 【Programming Bruce Lee】
Realization effect
Merge any string array , And keep the original order of strings for merging and de duplication , The effect is shown in the figure :
The implementation code is as follows
First, simply implement the merging between two string arrays , recycling js The implementation of recursive call with variable parameters of n A combination of string arrays
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Dragon brother </title>
</head>
<body>
<script type="text/javascript"> /** * Merge in order n Array of strings and remove duplicates * @param {...any} strArrys // Array of multiple character strings */ function mergeStrArrays(...strArrys) {
if (!strArrys) {
return []; } // Recursive export if (strArrys.length < 2) {
return strArrys[0] ? strArrys[0] : []; } // Recursively call return mergeStrArrays(mergeStrArray(strArrys.pop(), strArrys.pop()), ...strArrys); } /** * Merge two string arrays in order and remove duplicates * @param {Array} strArry1 Array of strings * @param {Array} strArry2 Array of strings * @returns */ function mergeStrArray(strArry1, strArry2) {
if (strArry2.length > strArry1.length) {
// Guarantee strArry1 Than strArry2 Long let strT = strArry2; strArry2 = strArry1; strArry1 = strT; } for (let i = 0; i < strArry1.length; i++) {
if (strArry2[i] === undefined) {
break; } // If the corresponding position str1 Elements and str2 Elements are different and str1 There is no such element in the array , Add... At this location str2 Elements if (strArry1[i] !== strArry2[i]) {
let j = 0; for (j; j < strArry1.length; j++) {
if (strArry1[j] === strArry2[i]) {
break; } } if (j >= strArry1.length) {
strArry1.splice(i, 0, strArry2[i]); } } } return strArry1; } let str1 = ["aaa", "bbb", "ccc", "ddd", "eee"] let str2 = ["bbb", "ccc", "eee", "ffff"] let str3 = ["bbb", "ccc", "mmm", "ggg"] console.log(" The three string arrays are :"); console.log(str1); console.log(str2); console.log(str3); console.log(" After the merger :"); console.log(mergeStrArrays(str1, str2, str3)); </script>
</body>
</html>
边栏推荐
- Graduation project - campus old thing recycling system based on stm32
- [data and Analysis Visualization] data operation in D3 tutorial 3-d3
- HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
- Perfect square
- Ijkplayer source code --- decode
- Prometheus install and register services
- Opencvsharp4 handwriting recognition
- 專業的數據庫管理軟件:Valentina Studio Pro for Mac
- Principle and steps of principal component analysis (PCA)
- Simple use of leaflet - offline map scheme
猜你喜欢
專業的數據庫管理軟件:Valentina Studio Pro for Mac
Summary of the latest IOS interview questions in June 2020 (answers)
[data analysis and visualization] key points of data drawing 4- problems of pie chart
02 optimize the default structure of wechat developer tools
Professional database management software: Valentina Studio Pro for Mac
数仓笔记|针对客户维度建模需要关注的5个因素
Summary of innovative ideas of transformer model in CV
Uni app Foundation
Principle and steps of principal component analysis (PCA)
How can intelligent safe power distribution devices reduce the occurrence of electrical fire accidents?
随机推荐
[reading papers] deep learning face representation from predicting 10000 classes. deepID
Digital IC Design -- FIFO design
Example 4 linear filtering and built-in filtering
[data analysis and visualization] key points of data drawing 8- use of circular bar chart
MySQL index
Perfect square
03 recognize the first view component
Opencvshare4 and vs2019 configuration
Advanced stair climbing
Vant框架中关于IndexBar索引栏的CDN单页面引用,无法正常展示
wx.createSelectorQuery()在components获取Dom节点的使用
Binary tree initialization code
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
A wechat app for shopping
Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
vant实现移动端的适配
[life science] DNA extraction of basic biological experiments
Hash tables: metaphrases
Detailed explanation of UCI datasets and their data processing (with 148 datasets and processing codes attached)
redis. Conf general configuration details