Python Program Finds Second Largest Number in List Without Sorting

Day 6 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find the second largest number in a list without using sorting. Finding the second largest element means identifying the number that is greater than all others except the maximum value. What the program does: • Takes a list of numbers as input • Handles edge cases like lists with fewer than two elements • Traverses the list only once for efficiency • Finds the second largest value without using built-in sorting How the logic works: If the list has fewer than two elements, the function returns None Two variables (first and second) are initialized to negative infinity The list is traversed element by element If the current number is greater than first, – second is updated with the previous first – first is updated with the current number If the number lies between first and second, second is updated Example: Original list: [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] Output: Second largest number = 6 Key learnings from Day 6: – Tracking multiple values in a single loop – Writing optimized logic without sorting – Understanding time complexity (O(n)) – Improving problem-solving skills in Python #100DaysOfCode #Day6 #Python #PythonProgramming #ProblemSolving #DSA #LearningInPublic #BTech #CSE #AIandML #DataStructures #CodingChallenge #CodeNewbie #TechJourney

  • text

To view or add a comment, sign in

Explore content categories