I have an array let arr = [ ['A1', 'B2'], ['B1', 'A1'], ['A2','B1'], ['A1', 'B1']]. Which has duplicate ['B1', 'A1'] and ['A1', 'B1'] of different direction still it should be removed and expected output should be [ ['A1', 'B2'], ['B1', 'A1'], ['A2','B1']].
-
5What have you tried so far?Mohammed Amir Ansari– Mohammed Amir Ansari2022-01-11 11:47:49 +00:00Commented Jan 11, 2022 at 11:47
-
You're going to likely need a custom function, any inbuilt function would look for an actual duplicate (as opposed to your pseudo-duplicate). Have you tried anything? It'll be easier for us to correct a reasonable attempt than to write something from scratch that may or may not do what you want.Andrew Corrigan– Andrew Corrigan2022-01-11 11:50:11 +00:00Commented Jan 11, 2022 at 11:50
-
Does this answer your question? javascript remove duplicates from array of objectsFurious Mountain– Furious Mountain2022-01-11 11:51:20 +00:00Commented Jan 11, 2022 at 11:51
-
1There’s work to be done, whether it is homework or an assigned task , the person to whom it was assigned is expected to complete it. Even if it seems the task is too hard or there isn’t a clear starting point, an effort should be made.Liam– Liam2022-01-11 12:00:22 +00:00Commented Jan 11, 2022 at 12:00
Add a comment
|
3 Answers
You could filter with a Set with normalized values (sorted and joined).
const
getValue = ([...a]) => a.sort().join(),
array = [['A1', 'B2'], ['B1', 'A1'], ['A2', 'B1'], ['A1', 'B1']],
result = array.filter((s => a => (v => !s.has(v) && s.add(v))(getValue(a)))(new Set));
console.log(result);
Comments
function removeRepeatedArray(arrays){
const map = new Map();
for(let i = 0; i < arrays.length; i++){
// convert each array to a unique key
// subarrays needs to be sorted first
const key = arrays[i].sort().join("");
if(map.has(key)){
// if exists replace it with undefined
// you can use splice method to remove the repeated array it index i
// I am using filter to keep the logic simple
// using splice will require updating i
arrays[i] = undefined;
}else{
map.set(key, arrays[i])
}
}
return arrays.filter(el => el !== undefined);
}
Comments
You can use Array.reduce() to get the required result, for each element in the input array we create a key.
We'll use this key to create an entry in a map object, and duplicate items will be eliminated since they will share the same key.
Once we have all items in our map, we'll use Object.values() to get the resulting (unique) array.
let arr = [ ['A1', 'B2'], ['B1', 'A1'], ['A2','B1'], ['A1', 'B1']]
const result = Object.values(arr.reduce((acc, el) => {
// Create a key based on what we consider a duplicate.
let key = el.sort().join("-");
acc[key] = acc[key] || el;
return acc;
}, {}));
console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Another approach is to use the lodash function uniqBy using the iteratee el => el.sort().join('-'):
let arr = [ ['A1', 'B2'], ['B1', 'A1'], ['A2','B1'], ['A1', 'B1']]
const result = _.uniqBy(arr, el => el.sort().join('-'));
console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
2 Comments
A1exandr Belan
Please tell me the name of the pattern used to solve this problem. When we group objects by properties and return the answer as
Object.values(...). I call it grouping by hash but I'm not sure if it's correct.Terry Lennox
It's similar to a group by hash, however we discard 'collisions', retaining only the first element returning a given hash, so it's probably more like the lodash _.uniqBy function (lodash.com/docs/4.17.15#uniqBy)