Optimizing Bulb Toggle Logic with HashSets in Java

Day 13/90 DSA Journey Today I worked on an interesting problem where each bulb toggles between ON and OFF based on its occurrences in the input list. ->Key Idea: If a number appears odd number of times → bulb remains ON If it appears even number of times → bulb turns OFF -> Approach: Used a List-based toggle logic Add element if not present Remove element if already present Finally, sort the result -> Time Complexity: O(n²) using List (due to contains & remove) -> Optimized Insight: This problem can be solved more efficiently using a HashSet in O(n) time. -> Takeaway: Understanding frequency and toggling patterns is very useful in solving real-world and DSA problems. #LeetCode #DSA #Java #Coding #ProblemSolving #Learning #TechJourney

  • text

To view or add a comment, sign in

Explore content categories