Filter array in javascript - javascript

I have an array which including all of the times as below :
["14:00-14:30" , "14:30-15:00", "15:00-15:30"]
as you can see this time slots, so basically, its from : 14:00 to 15:30
output will be : ["14:00-15:30"]
But if i have :
["14:00-14:30", "14:30-15:00", "15:30-16:00"]
in this case, the output would be : ["14:00-15:00", "15:30-16:00"]
My solution: convert all of this to a single array ["14:00", "14:30", "14:30", "15:00", ...]. And then forEach to each element, delete the one the have arr[i] === arr[i+1].
I got it working but I don't really like the way its. is there any better idea or how to use filter in this case ? Thanks.

Assuming the format of the array is correct:
const yourArray = ["14:00-14:30", "14:30-15:00", "15:30-16:00"];
const solution = yourArray.sort().reduce(
(acc, item, index) => {
if (index===0) {
acc.push(item);
return acc;
}
const currentValueParsed = acc[acc.length-1].split('-');
const newValueParsed = item.split('-');
if (currentValueParsed[1] === newValueParsed[0]) {
acc[acc.length-1] = `${currentValueParsed[0]}-${newValueParsed[1]}`;
return acc;
}
acc.push(item);
return acc;
}, []
);
console.log(solution); // ["14:00-15:00", "15:30-16:00"]
The code could be small, but I prefer being explicit.
We sort the array.
We add the first element to the final array.
And every new element, ee decide if we need to modify the last element of the solution array or add the new element to this array.
And because you look interested in evolve your original solution.
const yourArray = ["14:00-14:30", "14:30-15:00", "15:30-16:00"];
const solution = yourArray.sort()
.join('-')
.split('-')
.filter((item, pos, arr) => {
return pos === 0 || (item !== arr[pos - 1] && item !== arr[pos + 1]);
})
.reduce((acc, item, pos, arr) => {
if (pos % 2) {
acc.push(`${arr[pos - 1]}-${arr[pos]}`);
}
return acc;
}, []);
console.log(solution); // ["14:00-15:00", "15:30-16:00"]
Notes:
It is important to sort in the beginning.
pos % 2 is telling me if it is an even position.
I don't care arr[pos + 1] return undefined in the last item.

Filter won't work, since you're creating a new value, not just keeping existing values. Reduce would, though.
let start = null, end = null;
let finalAnswer = arr.reduce((result, current, i) => {
const [first, last] = current.split('-');
if (start === null) { start = first;}
if (first !== end && end !== null) { result.push(`${start}-${end}`); if (i === arr.length - 1) { result.push(current); }}
else if (i === arr.length - 1) { result.push(`${start}-${last}`); }
else { end = last; }
return result;
}, []);
I'm sure there's a cleaner way to do this -- I had to throw in edge cases more than I'd like -- but this works :)
The idea is that you keep track of the interval's start and end times; if the current interval's start equals the last interval's end, then update the end time to the current interval's. Otherwise, push the current start and end time and reset the counter for the next entry. The edge cases are to handle when the final entry either does or does not create its own new interval; if it does, push the entry as its own interval, and if not, push a new interval with the current start and the entry's end time.

this is really a reduce operation, so a solution could like:
const result = array
.sort() // if needed?
.map(tf => tf.split('-')) // make it easier to work with
.reduce((acc, currFrame, idx, arr) => {
let reducedFrame = acc[acc.length - 1] // get latest reduced frame
if (!reducedFrame || reducedFrame.length === 2) { // filled range or at start
reducedFrame = [currFrame[0]] // so start a new one
acc.push(reducedFrame)
}
const nextFrame = arr[idx + 1]
if (!nextFrame || nextFrame[0] !== currFrame[1]) { // at last frame or end of the current continuous frame
reducedFrame.push(currFrame[1]) // so end the reduced frame
}
return acc
}, [])
.map(tf => tf.join('-')) // put it back
or the dupe filter approach would work as well I believe, building off #Dalorzo:
const result = array
.join('-').split('-') // convert to array of singles
.filter((v,i) => array.indexOf(v) === i) // lose the dupes
.sort() // if needed (performs better here in this case)
.reduce((acc, cur, i, arr) => // join every 2
(i % 2 === 0)
? acc.concat([cur + '-' + arr[i + 1]])
: acc, [])

In order to create the desired array what I would do is to join and split like:
var arr =['14:00-14:30', '14:30-15:00', '15:30-16:00'];
var arr = arr.join('-').split('-');
The above will produce the array with all times and then you could remove the duplicates any way you want one way:
var result = arr.filter((v,i) => arr.indexOf(v) === i);

Related

using reduce to compose an array of cumulative strings

I want to return something like this ['4', '42','420'] for a given number: 420
I am using reduce to accumulate the number and using the index to concat the number in the index before. First two iterations are working fine, but in the third one I am having trouble thinking how to deal with the result since it is undefined => [ '4', '42', '4,42undefined' ]
Any thoughts?
function createArrayOfTiers(num) {
const onArray = String(num).split('')
onArray.reduce((acc, curr, index) => {
if (!index) return curr
const newAcc = [...acc, acc + curr[index -1]]
return newAcc
},[])
}
createArrayOfTiers(420)
If you want to collect the incremental digits of a number, I think a clearer approach would be to use .map (or Array.from) instead: on each iteration, slice the (stringified) number from index 0 to the current index.
function createArrayOfTiers(num) {
const strNum = String(num);
return [...strNum].map((_, i) => strNum.slice(0, i + 1));
}
console.log(createArrayOfTiers(420));
To fix your existing code, you'll want to access the previous element of the accumulator, not the previous element of curr (which doesn't really make sense), multiply it by 10, and add to it the digit being iterated over. Also make sure to return the constructed array:
function createArrayOfTiers(num) {
const onArray = String(num).split('')
return onArray.reduce((acc, curr, index) => {
if (!index) return Number(curr);
return [...acc, acc[index - 1] * 10 + Number(curr)];
}, [])
}
console.log(createArrayOfTiers(420));
You have the error because you forgot to take first number in the third iteration. You take acc + curr[index - 1] but forgot about acc + curr[index - 2].
To fix it you can slice numbers from first to current and then join them.
function createArrayOfTiers(num) {
const onArray = String(num).split("");
return onArray.reduce((acc, curr, index, arr) => {
const newAcc = [...acc, arr.slice(0, index + 1).join("")];
return newAcc;
}, []);
}
console.log(createArrayOfTiers(420));
Try this:
function createArrayOfTiers(num) {
const str = String(num);
const res = str.split('').reduce((acc, curr, index) => {
acc = [...acc, str.substring(0,index)+curr];
return acc;
}, []);
return res;
}
console.log( createArrayOfTiers(420) );
You could map the stringed value by using a closure of a string which stores the value of the last result.
function createArrayOfTiers(num) {
return Array.from(num.toString(), (s => v => s += +v)(''));
}
console.log(createArrayOfTiers(420));
The reduce aspect ...
function createArrayOfTiers(num) {
return [...num.toString()].reduce((r, v) => [...r, (r[r.length - 1] || '') + v], []);
}
console.log(createArrayOfTiers(420));
my way
const createArrayOfTiers = arg =>
[...String(arg)].reduce((r,c,i)=>[...r, (r[i-1]||'')+c],[])
console.log('420 -->' ,JSON.stringify( createArrayOfTiers(420) ));
console.log("'elisa' -->" ,JSON.stringify( createArrayOfTiers('elisa') ));
.as-console-wrapper{max-height:100% !important;top: 0;}
thanks to CertainPerformance in this thread I understood that I was trying to access the index of an inexistent array in curr therefore I needed to access the index of the acc value and came up to this solution using an updated version of my code:
function createArrayOfTiers(num) {
const onArray = String(num).split('')
const reducer = onArray.reduce((acc, curr, index) => {
if (!index) return [curr]
const newAcc = [...acc, acc[index -1] + curr]
return newAcc
},[])
return reducer
}
createArrayOfTiers(420)
I also transformed curr to an array because in case of only one digit, should return an array as well.
Thanks to all who gave brilliant ideas!!

Javascript - Delete all duplicates from array

I have problem with delete all duplicate in array.
Array = [1,1,2,2,3]
Every solution, what I found, haves result this
Array = [1,2,3]
But I need this
Array = [3]
How can I do this?
You can first iterate over the array once to obtain a Map of the frequencies of each item and then filter to find the elements that only appeared once.
const arr = [1,1,2,2,3];
const freq = arr.reduce((acc,curr)=>(acc.set(curr,(acc.get(curr)||0)+1),acc),new Map);
const res = arr.filter(x => freq.get(x) === 1);
console.log(res);
You could store an object for occurences of each element and get the elements that have the occurence of 1
const arr = [1, 1, 2, 2, 3]
const occurrences = arr.reduce((acc, el) => {
acc[el] = (acc[el] || 0) + 1
return acc
}, {})
const res = Object.entries(occurrences)
.filter(([el, time]) => time === 1)
.map(([el]) => +el)
console.log(res)
Unlike some of the other solutions, this allows you to make a single loop over the array, rather than a reduce followed by a filter and/or map loop. That said, there are trade-offs in readability and other condition checks, and it plays a bit fast and loose with the semantic intention of a reduce, so it might be a wash in terms of benefits.
const myArray = [1,1,2,2,3];
const dupesRemoved = myArray.reduce((acc, cur, idx, src) => {
if (!acc.dupes.has(cur)) {
if (acc.singleInstances.has(cur)) {
acc.singleInstances.delete(cur);
acc.dupes.add(cur);
} else {
acc.singleInstances.add(cur);
}
}
if (idx === src.length - 1) {
return [...acc.singleInstances];
}
return acc;
}, { singleInstances: new Set(), dupes: new Set() });
console.log(dupesRemoved);
Here is a simple and short solution:
let arr = [1,1,2,2,3];
let filtered_arr = arr.filter(v => arr.indexOf(v) === arr.lastIndexOf(v));
console.log(filtered_arr);

How to adjust return values of map() function?

I have been trying to make a excercise in the course I am taking. At the end, I did what was asked, but I personally think I overdid too much and the output is not convenient -- it's a nested array with some blank arrays inside...
I tried to play with return, but then figured out the problem was in the function I used: map always returns an array. But all other functions, which are acceptable for arrays (in paticular forEach and I even tried filter) are not giving the output at all, only undefined. So, in the end, I have to ask you how to make code more clean with normal output like array with just 2 needed numbers in it (I can only think of complex way to fix this and it'll add unneeded junk to the code).
Information
Task:
Write a javascript function that takes an array of numbers and a target number. The function should find two different numbers in the array that, when added together, give the target number. For example: answer([1,2,3], 4) should return [1,3]
Code
const array1 = [1, 2, 3];
const easierArray = [1, 3, 5] //Let's assume number we search what is the sum of 8
const findTwoPartsOfTheNumber = ((arr, targetNum) => {
const correctNumbers = arr.map((num, index) => {
let firstNumber = num;
// console.log('num',num,'index',index);
const arrayWeNeed = arr.filter((sub_num, sub_index) => {
// console.log('sub_num',sub_num,'sub_index',sub_index);
if (index != sub_index && (firstNumber + sub_num) === targetNum) {
const passableArray = [firstNumber, sub_num] //aka first and second numbers that give the targetNum
return sub_num; //passableArray gives the same output for some reason,it doesn't really matter.
}
})
return arrayWeNeed
})
return correctNumbers;
// return `there is no such numbers,that give ${targetNum}`;
})
console.log(findTwoPartsOfTheNumber(easierArray, 8));
console.log(findTwoPartsOfTheNumber(array1, 4));
Output
[[],[5],[3]]
for the first one
You can clean up the outpu by flatting the returned arrays :
return arrayWeNeed.flat();
and
return correctNumbers.flat();
const array1 = [1, 2, 3];
const easierArray = [1, 3, 5] //Let's assume number we search what is the sum of 8
const findTwoPartsOfTheNumber = ((arr, targetNum) => {
const correctNumbers = arr.map((num, index) => {
let firstNumber = num;
// console.log('num',num,'index',index);
const arrayWeNeed = arr.filter((sub_num, sub_index) => {
// console.log('sub_num',sub_num,'sub_index',sub_index);
if (index != sub_index && (firstNumber + sub_num) === targetNum) {
const passableArray = [firstNumber, sub_num] //aka first and second numbers that give the targetNum
return sub_num; //passableArray gives the same output for some reason,it doesn't really matter.
}
})
return arrayWeNeed.flat();
})
return correctNumbers.flat();
// return `there is no such numbers,that give ${targetNum}`;
})
console.log(findTwoPartsOfTheNumber(easierArray, 8));
console.log(findTwoPartsOfTheNumber(array1, 4));
However, using a recursive function could be simpler :
const answer = (arr, num) => {
if (arr.length < 1) return;
const [first, ...rest] = arr.sort();
for (let i = 0; i < rest.length; i++) {
if (first + rest[i] === num) return [first, rest[i]];
}
return answer(rest, num);
};
console.log(answer([1, 2, 3], 4));
console.log(answer([1, 3, 5], 8));
It looks like you are trying to leave .map() and .filter() beforehand, which you can't (without throwing an error). So I suggest a normal for approach for this kind of implementation:
const array1 = [1,2,3];
const easierArray = [1,3,5] //Let's assume number we search what is the sum of 8
const findTwoPartsOfTheNumber = (arr,targetNum) =>{
for(let index = 0; index < arr.length; index++) {
let firstNumber = arr[index];
// console.log('num',num,'index',index);
for(let sub_index = 0; sub_index < arr.length; sub_index++){
const sub_num = arr[sub_index];
// console.log('sub_num',sub_num,'sub_index',sub_index);
if (index != sub_index && (firstNumber + sub_num) === targetNum){
const passableArray = [firstNumber,sub_num]//aka first and second numbers that give the targetNum
return passableArray; //passableArray gives the same output for some reason,it doesn't really matter.
}
}
}
return `there is no such numbers,that give ${targetNum}`;
}
console.log(findTwoPartsOfTheNumber(easierArray,8));
console.log(findTwoPartsOfTheNumber(array1,4));
console.log(findTwoPartsOfTheNumber(array1,10));
I've just grab your code and changed map and filter to for implementation.
There doesn't appear to be any requirement for using specific array functions (map, forEach, filter, etc) in the problem statement you listed, so the code can be greatly simplified by using a while loop and the fact that you know that the second number has to be equal to target - first (since the requirement is first + second == target that means second == target - first). The problem statement also doesn't say what to do if no numbers are found, so you could either return an empty array or some other value, or even throw an error.
const answer = (list, target) => {
while (list.length > 0) { // Loop until the list no longer has any items
let first = list.shift() // Take the first number from the list
let second = target - first // Calculate what the second number should be
if (list.includes(second)) { // Check to see if the second number is in the remaining list
return [first, second] // If it is, we're done -- return them
}
}
return "No valid numbers found" // We made it through the entire list without finding a match
}
console.log(answer([1,2,3], 3))
console.log(answer([1,2,3], 4))
console.log(answer([1,2,3], 7))
You can also add all the values in the array to find the total, and subtract the total by the target to find the value you need to remove from the array. That will then give you an array with values that add up to the total.
let arr1 = [1, 3, 5]
const target = 6
const example = (arr, target) => {
let total = arr.reduce((num1, num2) => {
return num1 + num2
})
total = total - target
const index = arr.indexOf(total)
if (index > -1) {
return arr.filter(item => item !== total)
}
}
console.log(example(arr1, target))
Map and filter are nice functions to have if you know that you need to loop into the whole array. In your case this is not necessary.
So you know you need to find two numbers, let's say X,Y, which belong to an array A and once added will give you the target number T.
Since it's an exercise, I don't want to give you the working code, but here is a few hints:
If you know X, Y must be T - X. So you need to verify that T - X exists in your array.
array.indexOf() give you the position of an element in an array, otherwise -1
If X and Y are the same number, you need to ensure that their index are not the same, otherwise you'll return X twice
Returning the solution should be simple as return [X,Y]
So this can be simplified with a for (let i = 0; i < arr.length; i++) loop and a if statement with a return inside if the solution exist. This way, if a solution is found, the function won't loop further.
After that loop, you return [] because no solution were found.
EDIT:
Since you want a solution with map and filter:
findTwoPartsOfTheNumber = (arr, tNumber) => {
let solution = [];
arr.map((X, indexOfX) => {
const results = arr.filter((Y, indexOfY) => {
const add = Y + X
if (tNumber === add && indexOfX != indexOfY) return true;
else return false;
});
if (results > 0) solution = [X, results[0]];
})
return solution;
}

in the easiest and most concise way as possible

I want to sort an array values in an ascending or descending order without using sort().
I have created a function, however I am not satisfied with it.
I believe the code below could be much shorter and more concise.
Please let me know where to modify or you may entirely change the code too. Thank you in advance.
const func = arg => {
let flip = false;
let copy = [];
for(let val of arg) copy[copy.length] = val;
for(let i=0; i<arg.length; i++) {
const previous = arg[i-1];
const current = arg[i];
if(previous > current) {
flip = true;
copy[i] = previous;
copy[i-1] = current;
}
}
if(flip) return func(copy);
return copy;
};
l(func([5,2,8,1,9,4,7,3,6]));
If your input is composed of whole numbers, as in the example, pne option is to reduce the array into an object, whose keys are the numbers, and whose values are the number of times those values have occured so far. Then, iterate over the object (whose Object.entries will iterate in ascending numeric key order, for whole number keys), and create the array to return:
const func = arr => {
const valuesObj = {};
arr.forEach((num) => {
valuesObj[num] = (valuesObj[num] || 0) + 1;
});
return Object.entries(valuesObj)
.flatMap(
([num, count]) => Array(count).fill(num)
);
};
console.log(
func([5,2,8,1,9,10,10,11,4,7,3,6])
);
This runs in O(N) time.
To account for negative integers as well while keeping O(N) runtime, create another object for negatives:
const func = arr => {
const valuesObj = {};
const negativeValuesObj = {};
arr.forEach((num) => {
if (num >= 0) valuesObj[num] = (valuesObj[num] || 0) + 1;
else negativeValuesObj[-num] = (negativeValuesObj[-num] || 0) + 1;
});
return [
...Object.entries(negativeValuesObj).reverse()
.flatMap(
([num, count]) => Array(count).fill(-num)
),
...Object.entries(valuesObj)
.flatMap(
([num, count]) => Array(count).fill(num)
)
];
};
console.log(
func([5,2,8,1,-5, -1, 9,10,10,11,4,7,3,6, -10])
);
For non-integer items, you'll have to use a different algorithm with higher computational complexity.

Function to create array of arrays in javascript

Given an array with characters such as ["A","P","P","L","E","S","A","R","E"], I'm trying to create a function that will loop over the elements in the array, and create an array for each character, which will then be put into a master array. The master array will end up looking like[["A"],["P","P"],["L"],["E"],["S"],["A"],["R"],["E"] at the end, using a comparator function to check values (a,b) => a == b. Essentially, it needs to check each successive letter, and if they are the same, group into their own array within the master. The two A's should not be grouped together, since they aren't successive.
var arr = ["A","P","P","L","E"];
var master = [];
arr.sort(function(a,b){
for(var i = 0; i <= arr.length; i++){
compare each element to its successor. if successor is the same, create array of like elements ["A"],["C","C"],["B"] within final array
if(i + 1 == i){
master.push(i);
}
}
});
Just loop through the array and compare the last value to the current one.
DO NOT SORT -- that will change the order of your input array!
const coolFn = (arr) => {
return arr.reduce((rez, value, index) => {
if (index !== 0 && rez[rez.length - 1][0] === value) {
rez[rez.length - 1].push(value);
} else {
rez.push([value]);
}
return rez;
}, []);
}
const rez = coolFn('APPLES ARE NOT A BANANA PUDDING CUP'.split(''));
console.log(rez);
Can be accomplished pretty easily with Set to get unique items, reduce to turn transform and filter to find matching elements:
const arr = ["A","P","P","L","E"]
// get unique keys by expanding to a Set
const letters = [...new Set(arr)].reduce((p, c) => {
// add all matching elements from original array to aggregate
p.push(arr.filter(i => i === c))
return p;
}, []);
console.log(letters);
edit: sorry, I missed the requirement (hidden in a comment in your code) that you only add by comparing each element to its successor. My solution creates an array of each letter with its number of occurrences
You might do as follows;
var arr = ["A","P","P","L","E"],
result = arr.reduce((p,c) => {var fi = p.findIndex(a => a[0] === c);
fi === -1 ? p.push([c]) : p[fi].push(c);
return p;
},[]);
console.log(result);
As per the grouping only the sequential duplicates the following should do;
var arr = ["A","P","P","L","E","S","A","R","E"],
stickTheSame = a => a.reduce((p,c) => (p[p.length-1][0] === void 0 ||
p[p.length-1][0] === c ? p[p.length-1].push(c)
: p.push([c]),
p),[[]]);
console.log(JSON.stringify(stickTheSame(arr)));

Categories

Resources