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

Javascript 获取数组中唯一序列的最短方法,javascript,arrays,sequence,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()实现这一点吗? 如果没有,那我有什么建议吗 请注

我有一个基于数组生成模式的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]。子字符串(1)。拆分`,`.map(a=>~~a)
//询问原始序列作为参数
函数唯一序列(原始序列){
返回原始序列
.map(函数(值,索引){//获取每个数字之间的差值。
返回值-originalSequence[index-1];//类似于[1,2,3,2,1]=>[NaN,1,1,-1,-1]
})
.toString()//将结果解析为字符串格式=>“NaN,1,1,-1,-1”
.match(/N((,[0-9-]+)*?)\1*$/)[1]//我们寻找逗号分隔整数的最短模式
//(,\d+)从“NaN”之后开始,重复到
//字符串的结尾。结果类似=>“,1,1,-1,-1”
.substring(1)//删除第一个逗号=>“1,1,-1,-1”
.split(“,”)//转换为数组[“1”,“1”,“-1”,“-1”]
.map(函数(值){
返回parseInt(value);//将每个元素解析为整数[1,1,-1,-1]
});
}
log(f([1,2,3,4,5,4,3,2,1,2,3,4,5,4,3,2,1]))

log(唯一顺序([1,2,3,4,5,4,3,2,1,2,3,4,5,4,3,2,1]))
对不起,关于另一个答案。我跑得很快

// 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]。子字符串(1)。拆分`,`.map(a=>~~a)
//询问原始序列作为参数
函数唯一序列(原始序列){
返回原始序列
.map(函数(值,索引){//获取每个数字之间的差值。
返回值-originalSequence[index-1];//类似于[1,2,3,2,1]=>[NaN,1,1,-1,-1]
})
.toString()//将结果解析为字符串格式=>“NaN,1,1,-1,-1”
.match(/N((,[0-9-]+)*?)\1*$/)[1]//我们寻找逗号分隔整数的最短模式
//(,\d+)从“NaN”之后开始,重复到
//字符串的结尾。结果类似=>“,1,1,-1,-1”
.substring(1)//删除第一个逗号=>“1,1,-1,-1”
.split(“,”)//转换为数组[“1”,“1”,“-1”,“-1”]
.map(函数(值){
返回parseInt(value);//将每个元素解析为整数[1,1,-1,-1]
});
}
log(f([1,2,3,4,5,4,3,2,1,2,3,4,5,4,3,2,1]))

log(uniqueSequence([1,2,3,4,5,4,3,2,1,2,3,4,5,4,3,2,1])
您可以通过两个函数一起工作来实现这一点,其概念是我们希望从原始模式中获取一个增量更大的片段,并查看它是否在原始模式中均匀重复

function getUniquePattern(pattern) {
    // Test an incrementally large slice of the original pattern.
    for (i = 0; i < pattern.length; i++) {
        // Take a slice to test
        var attempt = pattern.slice(0, i);

        // Check if the slice repeats perfectly against the pattern
        if(sliceIsRepeatable(attempt, pattern)){
            // Return the matched pattern
            return attempt;
        }
    }

    // Return an empty array for failures
    return [];
}

function sliceIsRepeatable(slice, pattern) {
    // Slice length must be a multiple of the pattern's length
    if(pattern.length % slice.length !== 0 ) return false;

    for (i = 0; i < pattern.length; i++) {
        j = i % slice.length;
        if(pattern[i] !== slice[j]) {
            return false;
        }
    }

    return true;
}

getUniquePattern([1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1]);
函数getUniquePattern(模式){ //测试原始模式的增量大切片。 对于(i=0;i
您可以通过两个函数一起工作来实现这一点,其概念是我们希望从原始模式中获取一个增量更大的片段,并查看它是否在原始模式中均匀重复

function getUniquePattern(pattern) {
    // Test an incrementally large slice of the original pattern.
    for (i = 0; i < pattern.length; i++) {
        // Take a slice to test
        var attempt = pattern.slice(0, i);

        // Check if the slice repeats perfectly against the pattern
        if(sliceIsRepeatable(attempt, pattern)){
            // Return the matched pattern
            return attempt;
        }
    }

    // Return an empty array for failures
    return [];
}

function sliceIsRepeatable(slice, pattern) {
    // Slice length must be a multiple of the pattern's length
    if(pattern.length % slice.length !== 0 ) return false;

    for (i = 0; i < pattern.length; i++) {
        j = i % slice.length;
        if(pattern[i] !== slice[j]) {
            return false;
        }
    }

    return true;
}

getUniquePattern([1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1]);
函数getUniquePattern(模式){ //测试原始模式的增量大切片。 对于(i=0;i不确定这是不是最短的方法,但很简单。希望很容易理解,我已经把