-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSecondLargest.java
More file actions
45 lines (37 loc) · 934 Bytes
/
SecondLargest.java
File metadata and controls
45 lines (37 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.ub.interview;
/**
* Find second largest element in array
* @author Vikram
*
*/
public class SecondLargest {
/**
* Keep track of
* 1.arraysecond_largest and arraylargest
* 2.arraysecond_largest < arrayi < arraylargest
*/
private int largest ;
private int secondLargest ;
private int[] sampleArray;
public SecondLargest(int[] aSampleArray) {
largest = 0;
secondLargest = 0;
this.sampleArray = aSampleArray;
findSecondLargestElement();
}
private void findSecondLargestElement() {
for(int index=0 ;index<sampleArray.length;index++){
int comparator = sampleArray[index];
if(comparator > largest) {
secondLargest = largest;
largest = comparator;
}
else if(comparator < largest && comparator > secondLargest) {
secondLargest = comparator ;
}
}
}
public int getSecondLargest() {
return secondLargest;
}
}