I'm having trouble making this so I'm asking it here. I have an array like this:
var myArray = ["hi", "hi", "hi", "ha", "ha", "ho", "hu", "hu", "hu"]
What I want is storing the values that occur at least 3 times in a new array. So I should get:
var filteredArray = ["hi", "hu"]
Does anyone know how to do this?
I'm having trouble making this so I'm asking it here. I have an array like this:
var myArray = ["hi", "hi", "hi", "ha", "ha", "ho", "hu", "hu", "hu"]
What I want is storing the values that occur at least 3 times in a new array. So I should get:
var filteredArray = ["hi", "hu"]
Does anyone know how to do this?
Share Improve this question edited Jun 5, 2019 at 8:21 Naing Lin Aung 3,4304 gold badges33 silver badges51 bronze badges asked Jun 5, 2019 at 8:16 janjtejanjte 431 silver badge4 bronze badges 3- "What I want is storing the values" its not sorting its filtering – Maheer Ali Commented Jun 5, 2019 at 8:17
- do you want a single item of three or more or all item whos count is three or more? – Nina Scholz Commented Jun 5, 2019 at 8:18
- @NinaScholz Single item in a new array of the items that occur 3 times in the first array. – janjte Commented Jun 5, 2019 at 8:20
6 Answers
Reset to default 7You can do a .filter
without needing extra variables by using the optional second parameter which assigns the this
context of the callback. So, you can leverage it to keep a count of everything you've encountered and only allow items with a count of 3.
var myArray = ["hi", "hi", "hi", "ha", "ha", "ho", "hu", "hu", "hu"]
var filteredArray = myArray.filter(item => {
//check what the count so far is
var count = this[item] ? this[item] : 0;
//add one and assign it back as the new count
this[item] = ++count;
//only return the item if the count is 3
return count === 3;
}, {})
console.log(filteredArray)
For a more concise implementation, you can use the one suggested by @georg
var myArray = ["hi", "hi", "hi", "ha", "ha", "ho", "hu", "hu", "hu"]
var filteredArray = myArray.filter(item => (this[item] = ++this[item] || 1) === 3, {})
console.log(filteredArray)
You could take a hash table and count the occurences. Then filter if the count is three.
This approach works with a single loop: O(n)
var array = ["hi", "hi", "hi", "ha", "ha", "ho", "hu", "hu", "hu"],
hash = {},
result = array.filter(v => (hash[v] = (hash[v] || 0) + 1) === 3);
console.log(result);
You can do that in following steps:
- Create a function which takes an array and least no of times the element should repeat as argument.
- Remove the duplicates from the array using
Set
- Then use
filter()
on that. Insidefilter()
usefilter()
on the original array(with duplicate values) to get the array of same values. - Compare the length of that array with the count(second parameter)
var myArray = ["hi", "hi", "hi", "ha", "ha", "ho", "hu", "hu", "hu"];
const valWhichRepeat = (arr,count) =>
[...new Set(arr)].filter(x =>
arr.filter(a => a === x).length >= count
);
console.log(valWhichRepeat(myArray,3))
The above code don't have linear time plexity. If you want liner time plexity you can use reduce()
to build an object with count of elements and then use filter()
on its keys.
var myArray = ["hi", "hi", "hi", "ha", "ha", "ho", "hu", "hu", "hu"];
const valWhichRepeat = (arr,count) => {
const obj = arr.reduce((ac,a) => (ac[a] = ac[a] + 1 || 1,ac),{});
return Object.keys(obj).filter(x => obj[x] >= count);
}
console.log(valWhichRepeat(myArray,3))
The above still uses two loops. If you want to use only a single loop then you can push()
the elements inside reduce()
var myArray = ["hi", "hi", "hi", "ha", "ha", "ho", "hu", "hu", "hu"];
const valWhichRepeat = (arr,count) =>
arr.reduce(([obj,res],a) => {
obj[a] = obj[a] + 1 || 1;
if(obj[a] === count) res.push(a);
return [obj,res]
},[{},[]])[1]
console.log(valWhichRepeat(myArray,3))
Object.entries(myArray.reduce((res, cur) => {
res[cur] = (res[cur] || 0) + 1
return res
}, {})).filter(([k, v]) => v >= 3).map(([k, v]) => k)
var myArray = ["hi", "hi", "hi", "ha", "ha", "ho", "hu", "hu", "hu"]
var temp = {}
var filteredArray = []
for(let i = 0;i < myArray.length;i++){
if(temp[myArray[i]]){
temp[myArray[i]] += 1
}else{
temp[myArray[i]] = 1
}
}
for(let key in temp){
if(temp[key] >= 3){
filteredArray.push(key)
}
}
console.log(filteredArray);
function count() {
array_elements = ["hi", "hi", "hi", "ha", "ha", "ho", "hu", "hu", "hu"];
var filteredArray = [];
var current = null;
var cnt = 0;
for (var i = 0; i < array_elements.length; i++) {
if (array_elements[i] != current) {
if (cnt >= 3) {
filteredArray.push(current);
}
current = array_elements[i];
cnt = 1;
} else {
cnt++;
}
}
if (cnt >= 3) {
filteredArray.push(current);
}
document.write(filteredArray);
}
count()