Explorer
Node.js

Implement a `createSingleton` function. No matter how many times it is called, it should always return the same instance object.

Problem Statement

Implement a `createSingleton` function. No matter how many times it is called, it should always return the same instance object.

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: A singleton ensures only one instance exists throughout the application. šŸ’” Hint 2: Use a closure to store the instance privately. šŸ’” Hint 3: let instance; return () => { if (!instance) instance = { id: Date.now() }; return instance; }; āœ… Solution: ```javascript function createSingleton() { let instance; return () => { if (!instance) instance = { created: true }; return instance; }; } ```

Singleton Pattern

Medium
Implement a `createSingleton` function. No matter how many times it is called, it should always return the same instance object.
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
true