Thursday, September 18, 2025
HomeLanguagesJavascriptLodash _.sortedUniq()Method

Lodash _.sortedUniq()Method

The _.sortedUniq method is used to return the lowest index of the array where an element can be inserted and maintain its sorted order. Also, this method is like _.uniq except that it’s designed and optimized for sorted arrays. In _.uniq only the first occurrence of each element is kept and the order of result values is determined by the order they occur in the array.

Syntax:

_.sortedUniq(array)

Parameters: This method accepts only one parameters as mentioned above and described below:

  • array: This parameter holds the array to inspect.
  • Return Value: This method is used to return the new duplicate free array.

    Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library into the file.

    javascript




    // Requiring the lodash library 
    const _ = require("lodash"); 
          
    // Original array 
    let y = ([1, 1, 2, 3, 3, 4]);
          
    // Use of  _.sortedUniq() 
    // method 
    let index =  _.sortedUniq(y, [1, 1, 2]); 
          
    // Printing the output 
    console.log(index);

    
    

    Output: 
     

    [ 1, 2, 3, 4 ]
    

    Example 2:

    javascript




    // Requiring the lodash library 
    const _ = require("lodash"); 
          
    // Original array 
    let y = (['p', 'q', 'r', 't', 't', 'u', 's', 't', 't', 'v', 'w']);
          
    // Use of  _.sortedUniq() 
    // method 
    let index =  _.sortedUniq(y); 
          
    // Printing the output 
    console.log(index);

    
    

    Output:

    ['p', 'q', 'r', 't', 'u', 's', 't', 'v', 'w']
    

    Example 3:




    // Requiring the lodash library 
    const _ = require("lodash"); 
          
    // Original array 
    let y = (['chemistry', 'computer', 'computer'
      
    'english', 'geography', 'hindi', 'hindi'
      
    'maths', 'physics']);
          
    // Use of  _.sortedUniq() 
    // method 
    let index =  _.sortedUniq(y); 
          
    // Printing the output 
    console.log(index);

    
    

    Output:

    ['chemistry', 'computer', 'english', 
    'geography', 'hindi', 'maths', 'physics']
    

    Note: This will not work in normal JavaScript because it requires the library lodash to be installed.

    Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen 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!
    RELATED ARTICLES

    Most Popular

    Dominic
    32299 POSTS0 COMMENTS
    Milvus
    84 POSTS0 COMMENTS
    Nango Kala
    6660 POSTS0 COMMENTS
    Nicole Veronica
    11835 POSTS0 COMMENTS
    Nokonwaba Nkukhwana
    11895 POSTS0 COMMENTS
    Shaida Kate Naidoo
    6779 POSTS0 COMMENTS
    Ted Musemwa
    7053 POSTS0 COMMENTS
    Thapelo Manthata
    6735 POSTS0 COMMENTS
    Umr Jansen
    6741 POSTS0 COMMENTS