I have an array of objects where each object has a an id key. some of those objects have re-occuring id's and I want to remove those re-occuring objects.
So for instance:
let array = [{
"id": "123",
"country": "Brazil",
"address": "xyz abc",
"date": "Dec 17, 1995, 9:45:17 PM"
},
{
"id": "443",
"country": "Russia",
"address": "qwd qwd qwdqw",
"date": "Dec 17, 1965, 9:45:17 PM"
},
{
"id": "123",
"country": "Canada",
"address": "ktktkt",
"date": "Dec 17, 1925, 9:45:17 PM"
},
.
.
.
{}]
in the array above since index 0 and index 2 share the same id key value, I would like to completely remove them both from the array.
- I am looking for optimal code in terms of complexity, only linear (O(n)).
reduce. It might be a use case forfilter, but really, a simple loop keeping track of objects you've seen for a given ID before temporarily in aMap(or an object) is all you need. Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help.O(n)notlog(O)(I don't even know how to read this nor what it is).