Remove all elements which occurs more than one time [duplicate] - javascript

This question already has answers here:
Return numbers which appear only once (JavaScript)
(4 answers)
Find single occurrence of an element in an array
(3 answers)
Deleting both values from array if duplicate - JavaScript/jQuery
(9 answers)
Closed 8 months ago.
Suppose I have an array like [1,1,2,2,3,4]
I want to get [3,4] by using a function like function answer(data,n)

I would reduce() down to a map with the elements as keys, and boolean values to indicate whether the element has been seen before. Then it's just a matter of filtering out the entries that are duplicates:
const data = [1, 1, 2, 2, 3, 4];
const result = [
...data.reduce((a, v) => a.set(v, a.has(v)), new Map())
].filter(([_, v]) => !v).map(([k]) => k);
console.log(result);

Related

joining arrays with concat and using a loop [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
flatten an array without using .flat();
(16 answers)
Closed 9 months ago.
I run into the following problem, the following array: [[1,2,3],[4,5,6],[7,8,9]] I must go through it and get the following result [1, 2,3,4,5,6,7,8,9]. Currently I get the same result. Here my code:
let main= [[1,2,3],[4,5,6],[7,8,9]]
let result= [].concat(
main.map((e)=>{
return e
})
)
console.log(result)

I'm not getting the expected result of the variable I'm calling [duplicate]

This question already has answers here:
Does .sort function change original array?
(6 answers)
Why A and B are equal after sort()?
(3 answers)
How can you sort an array without mutating the original array?
(11 answers)
Closed 1 year ago.
I came across something strange. Here two variables which store two differents results:
const membersSortedByCurrentStageTotalPoints = teamMembers.sort((a, b) => compareRanking(a,b,currentIndex));
const membersSortedByRankingType = teamMembers.sort((a, b) => compare(a, b, "points"))
But when I'm using the variable membersSortedByCurrentStageTotalPoints I get the results of the variable membersSortedByRankingType.
Anyone would know why ?
From the MDN documentation for sort:
Return value
The sorted array. Note that the array is sorted in place, and no copy
is made.
The two variables both have the same value: A reference to the same array.

Create array of the duplicates from array containing duplicates in javascript? [duplicate]

This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
How to keep Duplicates of an Array
(4 answers)
Create new array with only the duplicate items in Javascript using for loops
(4 answers)
lodash: Get duplicate values from an array
(13 answers)
Closed 1 year ago.
For an array of objects ["a","b","a","c","d","b"] I'd like to get an array of the duplicates:
["a","b"].
Is there a way to do this efficiently, similar to set ([...new Set(myArray)];)?
You could still use a Set and filter the array by checking the existence.
const
items = ["a", "b", "a", "c", "d", "b"],
duplicates = items.filter((s => v => s.has(v) || !s.add(v))(new Set));
console.log(duplicates);

Merge multiple object values of array into 1 [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 2 years ago.
This is my array:
[
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3'
]
If want to check, if it has similar objects then I want it to be merged into one and return me array as:
[
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
]
How is this possible in JavaScript or TypeScript?
Use Set to remove duplicate values :
array = [ 'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'hBwNVTlluaagZ5Fqd6f0Ws3Vxtn1',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3',
'Ddy1QVOAO6SIvB8LfAE8Z0Adj4H3' ];
var result = [...new Set(array)];
console.log(result);

has() returns false for Set of multidimensional array? [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Javascript | Unique object in Set
(2 answers)
Closed 3 years ago.
I created a Set of 2D arrays like this:
let set = new Set([[1, 2], [4, 6]]);
now when I am doing set.has([1, 2]) it returns false.
How can I achieve this?
If you're going to compare objects or arrays (which aren't primitives, and so aren't ===), stringify everything in the Set first:
const set = new Set([[1, 2], [4, 6]]);
const setStringified = new Set([...set].map(JSON.stringify));
console.log(setStringified.has(JSON.stringify([1, 2])));

Categories