javascript - 获取数组中唯一序列的最短方法

标签 javascript arrays sequence

我有一段 JavaScript 代码,可以根据数组生成模式。

下面的数组:

[1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1]

将返回:

[1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1]

但是,我希望简化模式并只获得唯一的序列:

[1, 1, 1, 1, -1, -1, -1, -1]

有什么方法可以使用 slice()filter() 实现此目的吗? 如果没有,那么我该怎么做有什么建议吗?

请注意,数组模式和唯一序列的长度是可变的。

最佳答案

抱歉,关于另一个答案。我很快。

// Ask the original sequence as parameter
function uniqueSequence(originalSequence){

    return 
        originalSequence
        .map(function(value, index){                            // Get difference between each number.
            return value - originalSequence[index - 1];         // Somthing like [1,2,3,2,1] => [NaN, 1,1,-1,-1]
        })
        .toString()                                             // Parse that result to string format => "NaN,1,1,-1,-1"
        .match(/N((,[0-9-]+)*?)\1*$/)[1]                        // we look for the shortest pattern of comma-separated integers 
                                                                // (,\d+) starting right after "NaN" and repeating till 
                                                                // the end of the string. Result in something like => ",1,1,-1,-1"
        .substring(1)                                           // Remove the first comma => "1,1,-1,-1"                                    
        .split(',')                                             // Convert to array ["1","1","-1","-1"]
        .map(function(value){
            return parseInt(value);                             // Parse each element to integer [1,1,-1,-1]
        });
}

用最短的代码(ES6)

 f=_=>_.map((a,i)=>a-_[i-1]).toString().match(/N((,[0-9-]+)*?)\1*$/)[1].substring(1).split`,`.map(a=>~~a)

f=_=>_.map((a,i)=>a-_[i-1]).toString().match(/N((,[0-9-]+)*?)\1*$/)[1].substring(1).split`,`.map(a=>~~a)

// Ask the original sequence as parameter
function uniqueSequence(originalSequence){

    return originalSequence
        .map(function(value, index){                            // Get difference between each number.
            return value - originalSequence[index - 1];         // Somthing like [1,2,3,2,1] => [NaN, 1,1,-1,-1]
        })
        .toString()                                             // Parse that result to string format => "NaN,1,1,-1,-1"
        .match(/N((,[0-9-]+)*?)\1*$/)[1]                       // we look for the shortest pattern of comma-separated integers 
                                                                // (,\d+) starting right after "NaN" and repeating till 
                                                                // the end of the string. Result in something like => ",1,1,-1,-1"
        .substring(1)                                           // Remove the first comma => "1,1,-1,-1"                                    
        .split(',')                                             // Convert to array ["1","1","-1","-1"]
        .map(function(value){
            return parseInt(value);                             // Parse each element to integer [1,1,-1,-1]
        });
}

console.log(f([1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1]))
console.log(uniqueSequence([1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1]))

关于javascript - 获取数组中唯一序列的最短方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50838691/

相关文章:

javascript - 迭代与递归 : Calculate point position in sequence for known iteration

python - 使用唯一元素创建所有可能的子序列

sql-server - 序列作为列的默认值

javascript - 使用 webpack 替换 HTML 模板中的按钮

javascript - 多模态弹出窗口 vue.js

php - 按自定义顺序对数组的php数组进行排序

javascript - 合并两个数组并对最后一个数组进行排序

c - 输入字符串并以随机顺序打印它们

javascript - html Angular 表 - 类似枢轴的显示

javascript - 如何创建 JavaScript 延迟函数