8000 Add Finding the Second Largest Element (#198) · Surani02/Algorithms-Explanation@0721d60 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0721d60

Browse files
Add Finding the Second Largest Element (TheAlgorithms#198)
* Create Find Second Largest Element.md * Update Find Second Largest Element.md * Create Find Second Largest Element.md * Delete Find Second Largest Element.md * Update en/Selection Algorithms/Find Second Largest Element.md Co-authored-by: David Leal <halfpacho@gmail.com> * Create Find Second Largest Element.md * Update Find Second Largest Element.md Co-authored-by: David Leal <halfpacho@gmail.com>
1 parent c8508db commit 0721d60

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Find Second Largest Element
2+
3+
#### Problem statement
4+
5+
Given an unsorted array, write a function to find the second largest element in the array.
6+
7+
#### Approach
8+
9+
- Find the largest element in the array by traversing through the array using a loop and store the value in a variable (for ex: a )
10+
- Assign a variable to store the negative infinite value, which stores the least value (for ex: b )
11+
- Run a loop from zero to the size of the array.
12+
- Now check whether the current element is greater than variable "b" and also not equal to variable "a", which is the largest number in the array.
13+
- if the above condition is true, then the variable b stores the current element.
14+
15+
#### Time Complexity
16+
17+
- Best case: `O(n)`
18+
- Average case: `O(n)`
19+
- Worst case: `O(n)`
20+
21+
#### Space Complexity
22+
23+
Worst case: `O(1)`
24+
25+
#### Example
26+
27+
```txt
28+
arr = [2, 5, 3, 9, 12, 34, 25]
29+
Indexes: 0 1 2 3 4 5 6
30+
a = max(arr)
31+
(a = 34)
32+
b = float("-inf")
33+
34+
Traverse elements from i = 0 to i = 6
35+
i = 0
36+
Check if b < arr[i] (arr[0]) and arr[0] != a
37+
True : b = arr[0] (b = 2)
38+
39+
i = 1
40+
Check if b < arr[i] (arr[1]) and arr[1] != a
41+
True : b = arr[0] (b = 5)
42+
43+
i = 2
44+
Check if b < arr[i] (arr[2]) and arr[2] != a
45+
False : As b = 5 is greater than the current element arr[2] = 3
46+
continues with the loop
47+
48+
i = 3
49+
Check if b < arr[i] (arr[3]) and arr[3] != a
50+
True : b = arr[3] (b = 9)
51+
52+
i = 4
53+
Check if b < arr[i] (arr[4]) and arr[4] != a
54+
True : b = arr[4] (b = 12)
55+
56+
i = 5
57+
Check if b < arr[i] (arr[5]) and arr[5] != a
58+
False: As current element is equal to the variable "a" which stores the highest value in the array
59+
continues with the loop
60+
61+
i = 6
62+
Check if b < arr[i] (arr[6]) and arr[6] != a
63+
True : b = arr[6] (b = 25)
64+
65+
Now we get the value 25 in the variable "b", which is the second highest value in the array.
66+
```
67+
68+
#### Code Implementation Links
69+
70+
[JavaScript](https://github.com/TheAlgorithms/JavaScript/blob/master/Sorts/FindSecondLargestElement.js)
71+
72+
#### Video Explanation
73+
74+
[Video explaining 2 approaches](https://www.youtube.com/watch?v=Mv8jhYQEbkA)

0 commit comments

Comments
 (0)
0