Counting Frequency in Sorted Array with Binary Search

🚀 Day 8 of DSA Practice – Small Problem, Big Learning! Today I worked on a classic sorted array problem that looks simple… but has a powerful optimization behind it 👇 🔍 Problem: Count how many times a number appears in a sorted array 👉 Example: [1,1,2,2,2,2,3] → target = 2 → Output = 4 💭 How I approached it: ✅ Started with a Linear Scan (O(n)) – straightforward and reliable 🚀 Then optimized using Binary Search (O(log n)) → Found the first and last occurrence → Calculated frequency efficiently 🧠 Key Takeaway: Whenever you see a sorted array, think beyond brute force. 👉 Binary Search can turn an average solution into an optimized one. 💻 Code available here: 🔗 https://lnkd.in/g2wdfYzZ 📈 Consistency Check: Day 8 ✅ Learning something new every day and getting closer to stronger problem-solving skills 💪 #DSA #100DaysOfCode #JavaScript #CodingJourney #BinarySearch #LearnInPublic #TechGrowth

  • text

To view or add a comment, sign in

Explore content categories