Using reduce() to find the second largest element in the array - javascript

Is it possible to use reduce to find the second largest element in an array? Without using sort methods. Something like this:
Obs: The code below is for finding the largest value. I need to find the second largest value and would like to use reduce(). Without using the sort methods
array1 = [12, 16, 1, 5]
array2 = [8, 4, 5, 6];
function largestElement(array){
largestE = array.reduce((acc,currentValue) => currentValue > acc ? currentValue : acc )
return largestE
}
console.log(largestElement(array1))
console.log(largestElement(array2))
WITHOUT DOING THIS:
function secondLargest(array){
array.sort((a,b) => a-b)
return array[array.length-2]
}

Yes there are several ways you can do this. Here is one solution:
const array1 = [5, 5, 1, 1, 2, 3, 4];
const array2 = [12,16,1,5];
const reducer = (accumulator, currentValue, idx, a) => {
let larger = a.filter(n=>n>currentValue) // Fetch larger values than n
.filter((n,i,a) => a.indexOf(n) === i); // Get Unique values
if(larger.length === 1){
// if there is only one unique value larger than n, n is your answer.
return accumulator = currentValue;
} else {
return accumulator = accumulator; // else preserve the accumulator value
}
}
console.log(array1.reduce(reducer));
console.log(array2.reduce(reducer));

I think that the simpler the better,
I will just create two variables and compare each element of the array
First make two variables outside the loop
inside the loop just ask if current number is bigger than larger
if its bigger then assign the value to largest and make the secondLargest with the values of largest
2)if it is not larger than largest but it is smaller than secondLargest than pass that numbe to the secondLargest, please see the code below
function FindSecondLargest() {
largest = 0
secondLargest =0
for (let i =0 i<array.length, i++)
{
if(array[i] >largest)
{
secondLargest = largest
largest=array[i]
}else if(array[i] <secondLargest){
secondLargest=array[i]
}
}
return secondLargest;
}

Related

Finding the lonely integer - JavaScript [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 8 months ago.
Consider the array [1,2,2]
The array contains two unique values: 1, 2
The array contains duplicate values: 2
The lonely integer is 1
How can the lonely integer be returned?
For an array where you only care about grabbing the first integer which is lonely, you can check if the indexOf and lastIndexOf are the same. If they are, then it's lonely.
const array = [2, 2, 1, 3, 4, 3, 4];
const findLonely = (arr) => {
for (const num of arr) {
if (arr.indexOf(num) === arr.lastIndexOf(num)) return num;
}
return 'No lonely integers.';
};
console.log(findLonely(array));
If you have an array that has multiple lonely values, you can use this method to find all of the lonely values:
const array = [2, 2, 1, 3, 4, 3, 4, 6, 8, 8, 9];
const findAllLonely = (arr) => {
const map = {};
arr.forEach((num) => {
// Keep track of the number of time each number appears in the array
if (!map[num]) return (map[num] = 1);
map[num]++;
});
// Filter through and only keep the values that have 1 instance
return Object.keys(map).filter((key) => {
return map[key] === 1;
});
};
console.log(findAllLonely(array)); // expect [1, 6, 9]
const array = [0,1,2,2,1,5,4,3,4,3,2];
let lonely = array.filter((item,index)=> array.indexOf(item) === array.lastIndexOf(item));
console.log(lonely);
Working Demo :
// Array with duplicates
const arrWithDuplicates = [1, 2, 2];
var result = arrWithDuplicates.sort().filter((x,i,arr) => x !== arr[i+1] && x !== arr[i-1]);
console.log(result); // [1]
For each element your can use .filter() to help count the how many times the element is repeated. Then use .filter() again to return only those elements that appear once.
const nums = [1,2,2,3,4,4,4,5,5,6,7,7,7,8,8];
const singles = nums.filter(
//count how many times each element appears
num => nums.filter(n => n === num)
//return only those with freq. of 1
.length === 1
);
console.log( singles );
//OUTPUT: [ 1, 3, 6 ]
Use a 'for' loop with the filter function to loop through the array and only return the value that appears once
const arr = [0, 1, 2, 2, 1];
let unique;
for(var i = 0; i < arr.length; i++) {
if(a.filter(x => x == arr[i]).length == 1) {
unique = arr[i]
}
}
return unique;
the code below solves the challenge with O(n) complexity
function lonelyinteger(a) {
let result;
a.every((e)=>{
if(a.filter(x=>x==e).length==1) {
result = e;
return false;
}return true;
})
return result;
}
O(n) complexity
function lonelyinteger(a) {
for (let i = 0; i < a.length; i++) {
const count = a.filter((v) => v === a[i]).length;
if (count === 1) {
console.log(a[i]);
return a[i];
}
}
}
If there is multiple unique number in an array = [1,2,3,4,5,3,2,1] here 4 and 5 both are unique ,there is two lonely integer so the output should be like this result = [4,5]. In case of single unique integer we can return the result as result = [3] or result = 3. The below code snippet will solve both the scenario.
const array = [1,2,3,4,5,3,2,1]
let result = []
array.every(e => {
if(array.filter(x => x == e).length == 1) {
result.push(e)
}
return true
})
console.log(result)
Explanation: step by step
Your desire array from where you need to get the lonely integer.
We defined result as an array.
You can use simple for loop or array forEach (learn about forEach).
We are using array filter method (learn about filter) to get our work done. Here array.filter(x => x == e) this will result when the value of e is 1 (first element of the array) then the output will be [1,1].
So for 1 the .length == 1 will return false. This process will continue to get false and the if condition will not get executed until a the 'e' became 4 (4th element of the main array).
When 'e' became 4 then the result of array.filter(x => x == 4) will be [4] so the condition array.filter(x => x == e).length == 1 will be true and the if condition will execute. And inside that we are pushing the value 4 to the result array. You can add a next line return false to stop the execution and you will get only one single lonely integer.
return true is required here only if you're using the every method (learn about array every method)
Play with the code to get better understanding or comment if you've some question about this solution. Please give a up-vote if this answer is helpful.

Split array into arrays of numbers where the sum is equal to a specific target

I need to create a function that take as parameter an array and a target. It should return an array of arrays where the sum of these numbers equals to the target
sumPairs(array, target) {
}
For example:
sumPairs([1, 2, 3, 4, 5], 7) // output : [[2, 5], [3, 4]]
I know I have to use map(), and probably reduce(), set(), or filter() maybe (I read their documentation in MDN but still cant find out). I tried some ways but I can't get it.
If you guys could help me to find out how to dynamically create arrays and push them into a new array..
I read there some solutions (Split array into arrays of matching values) but I hate to just use created functions without knowing what they really do or how they work.
Some very basic code for achieving it, Just run all over combinations and conditionally add the items you want.
function sumPairs(array, target) {
var res = [];
for(var i = 0; i < array.length; i++){
for(var j = 0; j < array.length; j++){
if(i!=j && array[i]+array[j]==target &&
res.filter((x)=> x[0] == array[j] && x[1] == array[i]).length == 0 )
res.push([array[i], array[j]]);
}
}
return res;
}
var result = sumPairs([1, 2, 3, 4, 5], 7);
console.log(result);
Option 2 - see this answer for more options (like using reduce)
function sumPairs(array, target) {
return array.flatMap(
(v, i) => array.slice(i+1).filter(w => (v!=w && v+w==target)).map(w=> [w,v])
);
}
var result = sumPairs([1, 2, 3, 4, 5], 7);
console.log(result);
"The exercise says that it sould be arrays of pairs that sum the
target value so I think only 2 items"
If you need a pair that matches a sum and you pick any number from the list, you are left with
the following equation to solve num + x = sum where we want to find x. E.g. if you picked 7 and the target sum is 10 then you know you are looking for a 3.
Therefore, we can first construct a counting map of the numbers available in our list linear (O(n)) time and then search for matches in linear time as well rather than brute forcing with a quadratic algorithm.
const nums = [1, 2, 3, 4, 5];
console.log(findSumPairs(nums, 7));
function findSumPairs(nums, sum) {
const countByNum = countGroupByNum(nums);
return nums.reduce((pairs, num) => {
countByNum[num]--;
const target = sum - num;
if (countByNum[target] > 0) {
countByNum[target]--;
pairs.push([num, target]);
} else {
countByNum[num]++;
}
return pairs;
}, []);
}
function countGroupByNum(nums) {
return nums.reduce((acc, n) => (acc[n] = (acc[n] || 0) + 1, acc), {});
}
Here's another implementation with more standard paradigms (e.g. no reduce):
const nums = [1, 2, 3, 4, 5];
console.log(findSumPairs(nums, 7));
function findSumPairs(nums, sum) {
const countByNum = countGroupByNum(nums);
const pairs = [];
for (const num of nums) {
const target = sum - num; //Calculate the target to make the sum
countByNum[num]--; //Make sure we dont pick the same num instance
if (countByNum[target] > 0) { //If we found the target
countByNum[target]--;
pairs.push([num, target]);
} else {
countByNum[target]++; //Didin't find a match, return the deducted num
}
}
return pairs;
}
function countGroupByNum(nums) {
const countByNum = {};
for (const num of nums) {
countByNum[num] = (countByNum[num] || 0) + 1;
}
return countByNum;
}
You can also sort your array and find all the pairs with given sum by using two pointer method. Place the first pointer to the start of the array and the second pointer to the end.
if the sum of the values at the two places is :
More than target: Decrement your second pointer by 1
Less than target: Increment your first pointer by 1
Equal to target: This is one possible answer, push them to your answer array and increment your first pointer by 1 and decrement your second pointer by 1.
This is more performant solution with complexity O(n*log(n))

How to get the sum of all duplicates in an array?

I am trying to calculate the sum of all duplicates in an array. For example:
duplicate([1,1,2,3,3]) --> should return 8.
I have written the following function to calculate the sum of duplicates in an array using JavaScript. Currently it is returning an array with duplicates one less than what they are present in the array.
function duplicate(arr) {
var sum = 0;
arr.sort();
var stack = [];
for(var i = 0; i < arr.length; i++){
if (arr[i] === arr[i+1]) {
stack.push(arr[i])
}
}
return stack;
}
console.log(duplicate([1,2,1,2,2,3,3]))
This is returning [ 1, 2, 2, 3 ]
How do I get the correct array and calculate the correct sum? I have to use Object for that?
To make the logic easier, you might filter out the non-duplicates by checking whether their indexOf in the array is equal to their lastIndexOf in the array:
function duplicate(arr) {
const duplicates = arr.filter(elm => arr.indexOf(elm) !== arr.lastIndexOf(elm));
return duplicates.reduce((a, b) => a + b);
}
console.log(duplicate([1,1,2,3,3])); // --> should return 8.
console.log(duplicate([1,2,1,2,2,3,3]));
Initially create an object where the keys will be the integer and their value will be the number of occurrence. Then if the number of occurrence is more than 1 , multiply the number with number of occurrence.
function duplicate(arr) {
let dupVal = 0;
let k = arr.reduce((acc, curr, index) => {
if (acc[curr] === undefined) {
acc[curr] = 1
} else {
acc[curr] += 1;
}
return acc
}, {});
for (let keys in k) {
if (k[keys] > 1) {
dupVal += parseInt(keys, 10) * k[keys]
}
}
return dupVal;
}
console.log(duplicate([1, 2, 1, 2, 2, 3, 3]))
Try This one
const arr = [1,1,2,3,3]
let dup = arr.filter((value, index)=>{
// creating a copy of main array
let copyarr = [].concat(arr)
// removing present value
copyarr.splice(index,1)
// after removing present value, if you still
// get the value in copied array that means
// it has duplicates
if(copyarr.indexOf(value)>-1){
return true
}
return false
})
// now add it using reduce
let sum = dup.reduce((acc, value)=> acc+value,0)
console.log(sum)
Copy above code and paste into chrome devTool. You will get the answer.
The problem is that you are matching value with immediate next value in array, in array that is sorted already it will work, but not on unsorted one. So try to sort the array first and then run your code.
Edit :
Looks like sorting is added in code,
But another condition => if there is number that is repeated more than twice it should be handled and only appear once in stack, if that is required.
This will : console.log(duplicate([1,2,1,2,2,3,3]))
Result this : [1,2,3]
function duplicate(arr) {
var sum = 0;
arr.sort();
var stack = [];
for(var i = 0; i < arr.length; i++){
if (arr[i] === arr[i+1]) {
if(stack.length == 0 || (arr[i] != stack[stack.length-1])){
stack.push(arr[i])
}
}
}
return stack;
}
you can use JS Array.reduce method to accomplish your requirement in a shorter way
function sumDplicates(arr) {
return arr.reduce(function(tot, val, index, _arr) {
if (_arr.lastIndexOf(val) > index || _arr.indexOf(val) != index)
return tot + val;
return tot
}, 0)
}
console.log(sumDplicates([1, 1, 2, 3, 3]));
console.log(sumDplicates([1, 2, 1, 2, 2, 3, 3]));
You can pursue your original sorting approach with a slight modification:
if (arr[i] === arr[i + 1] || arr[i] === arr[i - 1])
That is, check if the previous or the next element in the sorted array is equal to the current element for it to qualify as a duplicate.
The following solution accomplishes this with filter and reduce:
function duplicate(array) {
return array
.sort((a, b) => a - b)
.filter((a, i, arr) => (arr[i] === arr[i + 1] || arr[i] === arr[i - 1]))
.reduce((a, b) => a + b, 0);
}
console.log(duplicate([1, 1, 2, 3, 3]));
console.log(duplicate([1, 2, 1, 2, 3, 3]));
Array.reduce() and Array.lastIndexOf() will simply solve your problem.
function sum(arr)
{
return arr.reduce(function(sum, item){
return arr.lastIndexOf(item)!==arr.indexOf(item) ? sum+=item : sum;
},0)
}
console.log(sum([1,1,2,3,3]));
console.log(sum([1,2,3,4]));
console.log(sum([1,2,2,3,4]));
console.log(sum([1,1,2,2,3,3,4,4]));
Though I don't know much about JavaScript, If I were you, I would have simply kept a temporary array, which copies all the duplicate variables and then use that array for sum.
Also, if you want to add the particular number as many times as it appears, I will suggest creating a table like the one in sparse matrices
and then referring to it during addition.
This logic, though not space efficient, is very easy to implement.
Here is an approach with a single Array.reduce and nothing else. No Array.indexOf or Array.lastIndexOf. Although it might not be as concise it does not traverse the array looking for indexes multiple times nor does any Array.filter:
const sumDubs = arr => arr.reduce((r,c) => {
if(r[c]) {
r[c] += 1
r.sum += r[c] > 2 ? (r[c]*c) - ((r[c]-1)*c) : r[c]*c
} else r[c] = 1
return r
}, { sum: 0 }).sum
console.log(sumDubs([1, 1, 2, 3, 3])) // 8
console.log(sumDubs([1, 2, 1, 2, 2, 3, 3])) // 14
console.log(sumDubs([1, 2, 1, 2, 2, 3, 3, 3, 1, 2, 4, 4])) // 28
The idea is to keep track of the on-going sum via a property in the accumulator of the Array.reduce and simply keep calculating the sum based on which number is duplicated and more importantly how many times.

Reduce of empty array with no initial value after testing a specific use case

Variable min will contain the sum of the 4 smallest items in a given array.
Variable max will contain the sum of the 4 largest items in a given array.
JS:
function main() {
const arr = [1, 2, 3, 4, 5]
const min = arr.sort().filter((element, index, array) => element !== array[array.length - 1]).reduce((accumulator, currentValue) => {
return accumulator + currentValue
})
const max = arr.sort().filter((element, index, array) => element !== array[0]).reduce((accumulator, currentValue) => {
return accumulator + currentValue
})
console.log(min, max)
}
main()
As expected, [1,2,3,4,5] will result in 10, 14. However, if the given array is [5,5,5,5,5], the program will return TypeError: Reduce of empty array with no initial value.
Why is this?
Thanks.
When all elements are the same,
the condition element !== array[array.length - 1] will be false for all elements, because all elements are the same as the last.
Therefore the result of the filter(...) will be an empty array,
and so you get the error that you got.
In fact this implementation is quite flawed.
It's better to work with the index rather than the element values:
function main(arr) {
const count = 4;
const sorted = arr.sort((a, b) => a - b);
const sum = (accumulator, currentValue) => accumulator + currentValue;
const min = sorted
.filter((element, index) => index < count)
.reduce(sum);
const max = sorted
.filter((element, index) => index >= arr.length - count)
.reduce(sum);
console.log(min, max);
}
main([1, 2, 3, 4, 5]);
main([5, 5, 5, 5, 5]);
I slipped in some other improvements as well:
Make the array a parameter of the function, for easier testing
Don't sort the array twice, once is enough
As #Andrew pointed out in a comment, arr.sort() does not sort integers properly, you need to pass it a comparator function to get the intended effect
Reduce duplicated logic: extracted sum function and count variable
Replace code block with inlined lambda expression

JavaScript Arrays : Keep only values that are present an odd number of times, including once

Hey guys so for example I have an array:
myArray[5,4,1,2,1,4,5,6,7,8,9,10,1,2,1,5,3,2]
I'm sorting that array:
[1, 1, 1, 1, 10, 2, 2, 2, 3, 4, 4, 5, 5, 5, 6, 7, 8, 9]
And I want to delete only the two duplicates so the array I want will be
[10,2,3,5,6,7,8,9]
So i'm using splice:
for (var index = 0; index < myArray.length +1; ++myArray) {
if(myArray[index+1]== myArray[index]) {
myArray.splice(index);
myArray.splice[index+1];
}
}
But when I'm pushing more of the of the numbers, the results seem unpredictable
How to do this properly?
To clarify: The purpose is to eliminate the numbers which repeat an even number of times.
Here's another method, which checks for an odd number of elements by subtracting the indexOf the key from the lastIndexOf the key after sorting:
var myArray = [5,4,1,2,1,4,5,6,7,8,9,10,1,2,1,5,3,2];
var result = myArray.sort().filter(function(key, idx) {
return myArray.indexOf(key) === idx && //handle first instance only
(myArray.lastIndexOf(key) - myArray.indexOf(key)) % 2 === 0;
});
console.log(result);
Here is an ECMAScript2015 solution:
var myArray = [5,4,1,2,1,4,5,6,7,8,9,10,1,2,1,5,3,2];
var count = myArray.reduce((count, num) =>
(count[num] = (count[num] || 0) + 1, count), {});
myArray = Object.keys(count).filter(num => count[num] % 2).map(Number);
console.log(myArray);
The count variable is an object of which the properties are the numbers in the original array. The value for each of these properties is the number of occurrences of that number in the original array.
The keys are then iterated to get only those into the final array that have a value (i.e. occurrences) that is odd. As object properties are iterated in numerical order (when numerical), the result is automatically sorted numerically.
About your code:
The for loop you have, has some issues:
for (var index = 0; index < myArray.length +1; ++myArray) {
if(myArray[index+1]== myArray[index]) {
myArray.splice(index);
myArray.splice[index+1];
}
}
Certainly you don't want to increment myArray, but index.
The boundary condition should not be length+1 but length-1 as in the body you have myArray[index+1] and don't want to go out of bounds there.
But more importantly, doing splice in your for loop will make the elements shift position, and as you then still increment index, you will skip elements.
In short, you should not use splice in such a loop. You can solve this by going in the reverse direction, and start at the end of the array working towards the beginning.
But the above proposed code does not have this problem and also saves you the step of sorting.
You can do this with two reduce and Object.keys(). First add values to object and then check each value with % 2 and add to array.
var myArray = [5,4,1,2,1,4,5,6,7,8,9,10,1,2,1,5,3,2];
var obj = myArray.reduce(function(o, e) {
o[e] = (o[e] || 0)+1;
return o;
}, {})
var result = Object.keys(obj).reduce(function(r, e) {
if(obj[e] % 2) r.push(Number(e));
return r;
}, []);
console.log(result)

Categories

Resources