Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Longest consecutive run

    Longest consecutive run

    Mediumarrayhash set

    Given unsorted numbers, return the length of the longest run of consecutive values (like 4, 5, 6, 7) among them, in O(n).

    Example 1

    Input:  nums = [100,4,200,1,3,2]
    Output: 4

    The run 1, 2, 3, 4.

    Example 2

    Input:  nums = [0,3,7,2,5,8,4,6,0,1]
    Output: 9

    Constraints

    • 0 ≤ nums.length ≤ 10⁵
    • -10⁹ ≤ nums[i] ≤ 10⁹

    Submit checks your solution against 8 tests, including hidden ones.

    /**
     * @param {int[]} nums
     * @return {int}
     */
    function longestConsecutive(nums) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.