Number of Open Tabs

Last Updated : 8 Jun, 2026

You are given an array arr[], where each element is either a tab ID or the string "END". If a tab ID appears, toggle its state (open if it's closed, or close if it's open). If "END" appears, close all open tabs. Your task is to determine the number of tabs that remain open after all operations are completed.

Explanation:

Input: arr[] = ["1", "2", "1", "END", "2"]
Output: 1
Explanation: In the above test case, firstly tab 1st is opened then 2nd is opened then 1st is closed then all are closed then again 2nd is opened.

Input: arr[] = ["1", "2", "END"]
Output: 0
Explanation: 1st and 2nd tab is opened then both closed so zero tabs open at last.

Try It Yourself
redirect icon

[Naive Approach] Simulation with Array - O(n × t) Time and O(t) Space

Simulate tab switching behavior using an array to track currently open tabs. "END" clears all tabs. For any tab name, if already open, close it; if closed, open it. Return number of open tabs at end.

C++
#include <bits/stdc++.h>
using namespace std;

int countTabs(vector<string> &arr)
{
    // Stores the tabs that are currently open
    vector<string> openTabs;

    for (string &tab : arr)
    {
        // "END" closes all currently open tabs
        if (tab == "END")
        {
            openTabs.clear();
        }
        else
        {
            // Check whether this tab is already open
            auto it = find(openTabs.begin(), openTabs.end(), tab);

            if (it != openTabs.end())
            {
                // Tab is open, so toggle it off (close it)
                openTabs.erase(it);
            }
            else
            {
                // Tab is closed, so toggle it on (open it)
                openTabs.push_back(tab);
            }
        }
    }

    // Number of tabs left open
    return openTabs.size();
}

int main()
{
    vector<string> arr = {"1", "2", "1", "3", "END", "2", "4"};

    cout << countTabs(arr);

    return 0;
}
Java
// Java program to count open tabs after processing commands
import java.util.*;

class GfG {
    
    static int countTabs(List<String> arr) {
        // Stores the tabs that are currently open
        List<String> openTabs = new ArrayList<>();
        
        for (String tab : arr) {
            // "END" closes all currently open tabs
            if (tab.equals("END")) {
                openTabs.clear();
            } else {
                // Check whether this tab is already open
                int index = openTabs.indexOf(tab);
                
                if (index != -1) {
                    // Tab is open, so toggle it off (close it)
                    openTabs.remove(index);
                } else {
                    // Tab is closed, so toggle it on (open it)
                    openTabs.add(tab);
                }
            }
        }
        
        // Number of tabs left open
        return openTabs.size();
    }
    
    public static void main(String[] args) {
        List<String> arr = Arrays.asList("1", "2", "1", "3", "END", "2", "4");
        
        System.out.println(countTabs(arr));
    }
}
Python
# Python program to count open tabs after processing commands

def countTabs(arr):
    # Stores the tabs that are currently open
    openTabs = []
    
    for tab in arr:
        # "END" closes all currently open tabs
        if tab == "END":
            openTabs.clear()
        else:
            # Check whether this tab is already open
            if tab in openTabs:
                # Tab is open, so toggle it off (close it)
                openTabs.remove(tab)
            else:
                # Tab is closed, so toggle it on (open it)
                openTabs.append(tab)
    
    # Number of tabs left open
    return len(openTabs)

# Driver code
if __name__ == "__main__":
    arr = ["1", "2", "1", "3", "END", "2", "4"]
    
    print(countTabs(arr))
C#
// C# program to count open tabs after processing commands
using System;
using System.Collections.Generic;

class GfG {
    
    static int countTabs(List<string> arr) {
        // Stores the tabs that are currently open
        List<string> openTabs = new List<string>();
        
        foreach (string tab in arr) {
            // "END" closes all currently open tabs
            if (tab == "END") {
                openTabs.Clear();
            } else {
                // Check whether this tab is already open
                int index = openTabs.IndexOf(tab);
                
                if (index != -1) {
                    // Tab is open, so toggle it off (close it)
                    openTabs.RemoveAt(index);
                } else {
                    // Tab is closed, so toggle it on (open it)
                    openTabs.Add(tab);
                }
            }
        }
        
        // Number of tabs left open
        return openTabs.Count;
    }
    
    static void Main(string[] args) {
        List<string> arr = new List<string> { "1", "2", "1", "3", "END", "2", "4" };
        
        Console.WriteLine(countTabs(arr));
    }
}
JavaScript
// JavaScript program to count open tabs after processing commands

function countTabs(arr) {
    // Stores the tabs that are currently open
    let openTabs = [];
    
    for (let tab of arr) {
        // "END" closes all currently open tabs
        if (tab === "END") {
            openTabs = [];
        } else {
            // Check whether this tab is already open
            let index = openTabs.indexOf(tab);
            
            if (index !== -1) {
                // Tab is open, so toggle it off (close it)
                openTabs.splice(index, 1);
            } else {
                // Tab is closed, so toggle it on (open it)
                openTabs.push(tab);
            }
        }
    }
    
    // Number of tabs left open
    return openTabs.length;
}

// Driver code
const arr = ["1", "2", "1", "3", "END", "2", "4"];

console.log(countTabs(arr));

Output
2

[Expected Approach] Simulate using Hash Set - O(n) Time and O(t) Space

Simulate tab switching using a Hash Set for O(1) lookup and modification. "END" clears all tabs. For any tab, if present in set, remove it; if absent, insert it. Return number of open tabs at end.

  • Create hash set openTabs.
  • Traverse each string in arr: If tab == "END" then clear openTabs. Else if tab exists in openTabs, erase it. Else insert tab into openTabs.
  • Return openTabs.size().
C++
#include <bits/stdc++.h>
using namespace std;

int countTabs(vector<string> &arr)
{
    // Stores all currently open tabs
    unordered_set<string> openTabs;

    for (string &tab : arr)
    {
        // Close all tabs when END is encountered
        if (tab == "END")
        {
            openTabs.clear();
        }
        // If the tab is already open, close it
        else if (openTabs.count(tab))
        {
            openTabs.erase(tab);
        }
        // Otherwise open the tab
        else
        {
            openTabs.insert(tab);
        }
    }

    // Return the number of tabs still open
    return openTabs.size();
}

int main()
{
    vector<string> arr = {"1", "2", "1", "3", "END", "2", "4"};

    cout << countTabs(arr);

    return 0;
}
Java
// Java program to count open tabs after processing commands
import java.util.*;

class GfG {
    
    static int countTabs(List<String> arr) {
        // Stores all currently open tabs
        Set<String> openTabs = new HashSet<>();
        
        for (String tab : arr) {
            // Close all tabs when END is encountered
            if (tab.equals("END")) {
                openTabs.clear();
            }
            // If the tab is already open, close it
            else if (openTabs.contains(tab)) {
                openTabs.remove(tab);
            }
            // Otherwise open the tab
            else {
                openTabs.add(tab);
            }
        }
        
        // Return the number of tabs still open
        return openTabs.size();
    }
    
    public static void main(String[] args) {
        List<String> arr = Arrays.asList("1", "2", "1", "3", "END", "2", "4");
        
        System.out.println(countTabs(arr));
    }
}
Python
# Python program to count open tabs after processing commands

def countTabs(arr):
    # Stores all currently open tabs
    openTabs = set()
    
    for tab in arr:
        # Close all tabs when END is encountered
        if tab == "END":
            openTabs.clear()
        # If the tab is already open, close it
        elif tab in openTabs:
            openTabs.remove(tab)
        # Otherwise open the tab
        else:
            openTabs.add(tab)
    
    # Return the number of tabs still open
    return len(openTabs)

# Driver code
if __name__ == "__main__":
    arr = ["1", "2", "1", "3", "END", "2", "4"]
    
    print(countTabs(arr))
C#
// C# program to count open tabs after processing commands
using System;
using System.Collections.Generic;

class GfG {
    
    static int countTabs(List<string> arr) {
        // Stores all currently open tabs
        HashSet<string> openTabs = new HashSet<string>();
        
        foreach (string tab in arr) {
            // Close all tabs when END is encountered
            if (tab == "END") {
                openTabs.Clear();
            }
            // If the tab is already open, close it
            else if (openTabs.Contains(tab)) {
                openTabs.Remove(tab);
            }
            // Otherwise open the tab
            else {
                openTabs.Add(tab);
            }
        }
        
        // Return the number of tabs still open
        return openTabs.Count;
    }
    
    static void Main(string[] args) {
        List<string> arr = new List<string> { "1", "2", "1", "3", "END", "2", "4" };
        
        Console.WriteLine(countTabs(arr));
    }
}
JavaScript
// JavaScript program to count open tabs after processing commands

function countTabs(arr) {
    // Stores all currently open tabs
    let openTabs = new Set();
    
    for (let tab of arr) {
        // Close all tabs when END is encountered
        if (tab === "END") {
            openTabs.clear();
        }
        // If the tab is already open, close it
        else if (openTabs.has(tab)) {
            openTabs.delete(tab);
        }
        // Otherwise open the tab
        else {
            openTabs.add(tab);
        }
    }
    
    // Return the number of tabs still open
    return openTabs.size;
}

// Driver code
const arr = ["1", "2", "1", "3", "END", "2", "4"];

console.log(countTabs(arr));

Output
2


Comment