Given
arr = [1,5,3,5,1,5,6,6,6];
If I wanted to find the elements that appear, say x = 3
times in the array, how would I do so without the use of objects? i.e. 5,6. Via array methods preferably.
Given
arr = [1,5,3,5,1,5,6,6,6];
If I wanted to find the elements that appear, say x = 3
times in the array, how would I do so without the use of objects? i.e. 5,6. Via array methods preferably.
You should be able to achieve that using only filter()
, indexOf()
, and reduce()
:
function filterByCount(array, count) {
return array.filter((a, index) =>
array.indexOf(a) === index &&
array.reduce((acc, b) => +(a === b) + acc, 0) === count
);
}
const arr = [1, 5, 3, 5, 1, 5, 6, 6, 6];
console.log(filterByCount(arr, 3));
Note that this approach is fairly inefficient. With the use of classes like Map
, you could achieve this in O(n) time instead of O(n2) time.
Another less simple approach that achieves this in O(n log(n)) time is to sort the array then compare the difference between the first and last index of each value with the expected count
. This solution requires sort()
and filter()
. If you don't want to mutate the original array, then slice()
is required as well:
function filterByCount(array, count) {
// uncomment to avoid mutating the input array
return array/*.slice()*/.sort((a, b) =>
a - b
).filter((value, index, sorted) =>
(index === 0 || sorted[index - 1] !== value) &&
index + count - 1 < sorted.length &&
sorted[index + count - 1] === value &&
(index + count >= sorted.length || sorted[index + count] !== value)
);
}
const arr = [1, 5, 3, 5, 1, 5, 6, 6, 6];
console.log(filterByCount(arr, 3));
Just an idea, though, if you can sort the array, you can count the numbers that appear consecutively.
function findRepeatingNumbers(numbers, count) {
numbers.sort((a, b) => a - b);
const found = [];
let counter = 1;
for (let i = 1; i < numbers.length; i++) {
if (numbers[i - 1] == numbers[i]) {
counter += 1;
} else {
if (counter === count) {
found.push(numbers[i - 1]);
}
counter = 1;
}
}
if (counter == count) {
found.push(numbers[numbers.length - 1]);
}
return found;
}
console.log(findRepeatingNumbers([1, 5, 3, 5, 1, 5, 6, 6, 6], 3));
If you want to find the elements that appear, how many times in the array, you can easily know this following code. For example, here 6 is 3 times in this array.
Run the snippet check it out.
let arr = [1, 5, 3, 5, 1, 5, 6, 6, 6];
console.log((arr.join("").match(new RegExp("6", "g")) || []).length)