2017-12-21 10 views
0

私は大きなデータ配列を持っており、適切な値を抽出し、オールインワン重複除外を実装する信頼できる方法が必要です。Javascript Arrayから重複値を削除するだけですが、5回以上重複した値

公平値は私の配列に格納されているように、私は、少なくとも5回以上発生し、私の配列から値を抽出する必要が...

は、ここで私が得たものです:

var data=['1','1','1','1','1','1','1','5','5','5','5','6','7','7','7','7','7','7'] 

var uniqueArray = data.filter(function(elem, pos) { 
    return data.indexOf(elem) == pos; 
}); 
for (var i=0; i<uniqueArray.length;i++){ 
    console.log(uniqueArray); 
}; 

上記のコードは、追加しようとすると重複する値を削除するために正常に機能します。

var uniqueArray = data.filter(function(elem, pos) { 
    return data.indexOf((elem) == pos) > 5; 
}); 
for (var i=0; i<uniqueArray.length;i++){ 
    console.log(uniqueArray); 
}; 

これは機能しません...値は受け取りません。

何私が受け取ることを期待することは1つだけと7

Plzを助ける新しい配列であります!

ありがとうございます!

+0

ハッシュテーブルを実装してください!あなたは5つのインデックスが必要ですが、5以上のインデックスを付けます。 –

答えて

2

簡単な解決策は、各値が配列中に存在する回数を表す(以下スニペットでcountsと命名)インデックスを作成することであろう。

const data = ['1','1','1','1','1','1','1','5','5','5','5','6','7','7','7','7','7','7']; 
 

 
const counts = data.reduce((result, value) => { 
 
    result[value] = (result[value] || 0) + 1; 
 
    return result; 
 
}, {}); 
 

 
const result = Object.keys(counts).filter(value => counts[value] >= 5); 
 

 
console.log(result);

また、配列がソートされた、あなたはより多くの手続き溶液を用いてcountsオブジェクトの必要性を排除することができます。

const data = ['1','1','1','1','1','1','1','5','5','5','5','6','7','7','7','7','7','7']; 
 

 
let current; 
 
let count = 0; 
 
let result = []; 
 

 
for (let i = 0; i < data.length; i++) { 
 
    let value = data[i]; 
 
    
 
    if (value !== current) { 
 
    count = 0; 
 
    current = value; 
 
    } 
 
    
 
    if (++count === 5) { 
 
    result.push(value); 
 
    } 
 
} 
 

 
console.log(result);

+0

それは動作しているようです!どうもありがとう!このスニペットはどのようにパフォーマンス面でですか?かなり軽い解決策ですか? –

+0

さて、インデックスオブジェクトを作成するために追加のオーバーヘッドがあります。 (あなたの質問に対するすべての答えは、このアプローチを使用しています。)あなたの配列があなたの例のように常にソートされている場合、インデックスオブジェクトの必要性を取り除くより手続き的な解決策が考えられます。 –

0

最初に、値とその出現を含むmapを作成し、出現に基づいてmapから値を抽出します。

var data = ['1', '1', '1', '1', '1', '1', '1', '5', '5', '5', '5', '6', '7', '7', '7', '7', '7', '7'] 
 

 
var map = data.reduce(function(acc, item) { 
 
    if(acc[item]) acc[item] += 1; 
 
    else acc[item] = 1; 
 
    return acc; 
 
}, {}); 
 

 
var result = Object.keys(map).filter(function(key) { 
 
    return map[key] >= 5; 
 
}) 
 

 
console.log(result);

0

は、あなたが次のことを行うことができます

var data=['1','1','1','1','1','1','1','5','5','5','5','6','7','7','7','7','7','7'] 

// Create an index mapping of format <value in data array>: <number of occurances> 
var map = data.reduce((map, value)=>{ 
    if (map[value]) 
     map[value]++ // If occurred one or more times, we increment the occurrence count 
    else 
     map[value] = 1 // If first time occurrence, we set number of occurrences to 1 
    return map // return updated index map to reducer 
}, {}) 

// Then we iterate through the index mapping 
Object.keys(map).forEach((key)=>{ 
    if(map[key] > 5) console.log(key) // Note: Here we should print the key, not the map[key] 
}) 
0

、これを試してみてください:

var data=['1','1','1','1','1','1','1','5','5','5','5','6','7','7','7','7','7','7'] 
 

 
var counts = {}; 
 
data.forEach(function(item) { counts[item] = (counts[item] || 0)+1; }); 
 
var uniqueArray = Object.keys(counts).filter(function(key) { 
 
    return counts[key] >= 5; 
 
}); 
 
console.log(uniqueArray);

関連する問題