Sort duplicate numbers from array in Javascript [duplicate] - javascript

This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 10 months ago.
I had got a question in my exam as follow:
we need to create function that will receive 1 array of all positive number and return all the duplicate values of the array in sorted order.
Here is the solution that I had implemented:
function solution(arr) {
return arr.filter((value,index)=>arr.indexOf(value)!==index).sort()
}
But the code was rejected. Can someone tell me what could be more optimized solution for this problem in Javascript?

maybe because of this :
solution(['sada','sada','r',4,'r','r']) => ['r', 'r', 'sada']
or
solution([2,2,1,3,3,6,7,7,7]) => [2, 3, 7, 7]
your solution when array have same thing more than 2 times, return wrong array .

Related

The fastest and most efficient way to get repeated numbers in a nested array [duplicate]

This question already has answers here:
How to flatten nested array in javascript? [duplicate]
(27 answers)
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I know how to do this combining a lot of if...else statements but I need a faster and more efficient way.
I need a function that will run through a nested array and return the numbers that occur more than once in the nested array.
this doesn't work with nested arrays and even if you use flat(), the code still breaks when the duplicates in the array are more than 2
For example-
Lets call the name of the function deepSort(nestedArray)
where nestedArray is the nested array parameter
deepSort([[1,3,4,5], [4,7,9,1,3], [2,3,5], [1,2,3,4]]) //Returns 1,2,3,4,5
deepSort([[1,2,3], [4,5], [6,7,8], [2,9,0]]) //Returns 2
deepSort([[2,7,9], [4,3], [9,6,5], [1,4,3]]) //Returns 3,4,9
What I tried
function deepSort(nestedArray) {
const flatArr = nestedArray.flat().sort();
let results = []
for (let i = 0; i < flatArr.length - 1; i++) {
if (flatArr[i + 1] ==flatArr[i]) {
results.push(flatArr[i]);
}
}
return (results.filter((item,index) => results.indexOf(item) === index)).join()
}
Can this be optimized any more for speed and efficiency when handling larger values of data?

JavaScript How to declare 16 length array with default 0 value quick? [duplicate]

This question already has answers here:
Most efficient way to create a zero filled JavaScript array?
(45 answers)
Closed 3 years ago.
let array1 = Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
how to make like this array with shortest way ? is it possible to make shorter way ?
thank you for your answers.
You can use the .fill() method:
let array1 = new Array(16).fill(0);
As its name suggests, it fills the array with some desired value.

What does a and b stand for in the sorting function [duplicate]

This question already has answers here:
How does Javascript's sort() work?
(8 answers)
Closed 4 years ago.
I have started learning javascript a few days ago and I have a bad time understand what these lines:
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});
What is a and b equal to in this function?
Thank you very much for your help in advance
The sorting algorithm implement by sort() will call the specified function with various combinations of elements from array to determine which element should go first in the order. The two chosen element for comparison will be a and b.

Write a function that accepts an array of test scores as an argument then the function should return a number of scores that 80 or higher? [duplicate]

This question already has answers here:
How to filter array values greater than x
(4 answers)
Closed 4 years ago.
I'm still pretty new into JS but I'm having a hard time with this problem. I'm sure the answer is somewhat simple but i can't figure out what to put into the function expression after i've made my array of scores? Do i need to do a for loop?
Thanks
array = [20,30,40,50,60,70,80,92,93,95,98,100]
function testScores(array){
}
you can use array's filter method. please follow below definition.
function testScores(array){
return array.filter(ele=>ele>=80)
}
function testScores(array){
return array.filter(number => number >= 80);
}
This will return an array of scores more 80 and above.
e.g
let scores = [20,30,40,50,60,70,80,92,93,95,98,100];
testScores(scores)
If you just want the count of scores more than 80 you can do
testScores(scores).length

max of array in JavaScript [duplicate]

This question already has answers here:
Find the min/max element of an array in JavaScript
(58 answers)
Closed 5 years ago.
Given an array of values [1,2,0,-5,8,3], how can I get the maximum value in JavaScript?
I know I can write a loop and keep track of the maximum value, but am looking for a more code-efficient way of doing so with the JavaScript syntax.
You can use Math.max and ES6 spread syntax (...):
let array = [1, 2, 0, -5, 8, 3];
console.log(Math.max(...array)); //=> 8
You can use the following:
yourArray.reduce((max, value) => {return Math.max(max, value)});
The reduce will itterate over values in the array, at each time returning the maximum of all of the values before the current one.

Categories

Resources