functiongetUnique(arr){letuniqueArr=[];//loop through array for(letiofarr){if(uniqueArr.indexOf(i)===-1){uniqueArr.push(i);}}console.log(uniqueArr);}constarray=[1,2,3,2,3];
Both of these solutions are exponentially slow due to the need to search the growing unique array (includes and indexOf do a linear search). You can do the unique array in O(N) using a Set, Map or in this case a PoJo. The one liner using Set is:
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
To add context here for learning purposes, a POJO is an object that only contains data (as opposed to methods or internal state), most JavaScript codebases consider objects created using curly braces {} to be POJOs, however, more strict codebases sometimes create POJOs by calling Object.create(null) to avoid inheriting from the built-in Object class.
JavaScript Maps are an alternative to POJOs for storing data because they neither inherit keys from the Object class.
A downside is that objects are generally easier to work with than maps cause not all JS functions, frameworks, and libs support maps.
i.e. JSON.stringify() function doesn't serialize maps by default:
Set() does not allow duplicates in the first place so there is no need for me to remove duplicates just use Set from the beginning when it need to be unique.
The best solution of above is solution3, but i suggest a new solution if didn't know about Set
Create a new object and create a new key with valuetrue if you loop in array and it not existed, so if it not you will get the undefined value
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (8)
Both of these solutions are exponentially slow due to the need to search the growing unique array (
includes
andindexOf
do a linear search). You can do the unique array in O(N) using a Set, Map or in this case a PoJo. The one liner using Set is:To add context here for learning purposes, a POJO is an object that only contains data (as opposed to methods or internal state), most JavaScript codebases consider objects created using curly braces
{}
to be POJOs, however, more strict codebases sometimes create POJOs by callingObject.create(null)
to avoid inheriting from the built-inObject
class.JavaScript
Map
s are an alternative to POJOs for storing data because they neither inherit keys from theObject
class.A downside is that objects are generally easier to work with than maps cause not all JS functions, frameworks, and libs support maps.
i.e.
JSON.stringify()
function doesn't serialize maps by default:Best friends are options kk thanks
People should better lern how to use JavaScript Set() instead of using arrays which need to be unique.
So which solution are you using to Remove Duplicates From Array?
Set() does not allow duplicates in the first place so there is no need for me to remove duplicates just use Set from the beginning when it need to be unique.
It's a good suggestion if you care about the performance.
The best solution of above is solution3, but i suggest a new solution if didn't know about Set
Create a new object and create a new key with value true if you loop in array and it not existed, so if it not you will get the undefined value