_.uniq( array, [isSorted], [iterate] )
Parameters: This function accepts three parameters which are listed below:
- array: This parameter is used to hold the array of elements.
- isSorted: It is an optional parameter. This parameter is used to hold true for sorted arrays.
- iterate: It is an optional parameter that is used to hold the iterate function.
Return value: It returns an array of unique elements.
Passing a list of numbers to _.uniq() function: The ._uniq() function takes the element from the list one by one and checks whether it is in the resultant array (which is initially empty) by the ‘===’ operator. If it is present then it ignores it and checks the next element. Otherwise, since it is the first occurrence of the element so it is included in the resultant array.
Passing the second parameter as false to the _.uniq() function: If pass the second parameter as false along with the array then the _.uniq() function will work in a similar manner as in the first example. All the unique elements will be present in the resultant array.
Passing the second parameter as true to the _.uniq() function: If pass the second parameter as true along with the array then the _.uniq() function will not work in a similar manner rather it will perform any operation on the array. Hence, the resultant array will contain all the elements of the array passed in the same order in which it appeared in the passed array.
Passing words to the _.uniq() function: If passing the set of strings to the _.uniq() function then it will work in a similar manner as it will work with numbers etc. Therefore, the resultant array will contain only the first occurrence of all the repeated elements in the resultant array.
Note: These commands will not work in Google Console or in Firefox as these additional files need to be added which they didn’t have added. So, add the given links to your HTML file and then run them.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!