Easy Typescript Solution using Hashmap and Sorting with Full Explanation
IntuitionApproachComplexity
Time complexity: O(nlogn)


Iterating through orders: The function iterates through the orders array once, which has a length of n.
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity: $$O(n log n)$$ 
1. **Iterating through orders**: The function iterates through the `orders` array once, which has a length of `n`. During this iteration, it performs constant-time operations such as adding to sets and updating the map. This part contributes O(n) to the time complexity.

2. **Sorting tables and items**: After collecting unique tables and items, the function sorts them. The sorting operation for the tables (which can be at most `m` unique tables) and the items (which can be at most `k` unique items) takes O(m log m) and O(k log k) respectively. 

3. **Building the result array**: The function constructs the result array by iterating through the sorted tables and items. This involves nested loops, resulting in O(m * k) for this part.

Combining these components, the overall time complexity is O(n + m log m + k log k + m * k). In the worst case, if `m` and `k` are proportional to `n`, this can be simplified to O(n log n).
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(m * k)$$ 
1. **Sets and Map**: The function uses two sets and one map to store unique tables, unique items, and their counts. In the worst case, the space used by these data structures can be O(m + k) for the sets and O(m * k) for the map, where `m` is the number of unique tables and `k` is the number of unique items.

2. **Result array**: The result array will contain O(m * k) entries, as it includes a row for each table and a column for each item.

Thus, the overall space complexity is O(m * k), which dominates the space used by the sets and map. 
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```typescript []
function displayTable(orders: string[][]): string[][] {

    const tableSet: Set<string> = new Set();
    const itemSet: Set<string> = new Set();
    const map: Map<string, number> = new Map();

    for (const [name, table, item] of orders) {

        // find unique table using set
        tableSet.add(table);

        // find unique food item using set
        itemSet.add(item);
        const key: string = [table, item].join("&");

        // find the frequency of table&item using map
        map.set(key, (map.get(key) || 0) + 1);
    }

    // sort the tableSet elements and itemSet elements
    const tables: string[] = [...tableSet].sort((a, b) => +a - +b);
    const items: string[] = [...itemSet].sort();

    const result: string[][] = [];

    // push first row of "Table" in result
    result.push(["Table", ...items]);

    for (let table of tables) {

        // take each row with first element as table
        const row: string[] = [table];

        for (let item of items) {

            // find key for table&item to found multiple count from map
            const key: string = [table, item].join("&");
            const count: number = map.get(key) || 0;

            // push count as string in row
            row.push(count.toString());
        }

        // push each row in result
        result.push(row);
    }

    // return final answer result
    return result;
};
```
['Array', 'Hash Table', 'String', 'Sorting', 'TypeScript']