Skip to content

Commit 4526bb6

Browse files
raghav-jhapauljervis
authored andcommitted
BAEL-1586 Sum and Average in Java Array (eugenp#3718)
* Evaluation Article * CR comments resolved * BAEL-1586 Find Sum and Average in Java Array * Revert "CR comments resolved" This reverts commit c9bf88f. * Revert "Evaluation Article" This reverts commit 41ad303. * BAEL-1586 Removed Author Description * BAEL-1586 Find Sum And Avergae In Array * remove excess whitespace
1 parent 52d257a commit 4526bb6

6 files changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.array;
2+
3+
public class Find2ndLargestInArray {
4+
5+
public static int find2ndLargestElement(int[] array) {
6+
int maxElement = array[0];
7+
int secondLargestElement = -1;
8+
9+
for (int index = 0; index < array.length; index++) {
10+
if (maxElement <= array[index]) {
11+
secondLargestElement = maxElement;
12+
maxElement = array[index];
13+
} else if (secondLargestElement < array[index]) {
14+
secondLargestElement = array[index];
15+
}
16+
}
17+
return secondLargestElement;
18+
}
19+
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.array;
2+
3+
import java.util.Arrays;
4+
5+
public class FindElementInArray {
6+
7+
public static boolean findGivenElementInArrayWithoutUsingStream(int[] array, int element) {
8+
boolean actualResult = false;
9+
10+
for (int index = 0; index < array.length; index++) {
11+
if (element == array[index]) {
12+
actualResult = true;
13+
break;
14+
}
15+
}
16+
return actualResult;
17+
}
18+
19+
public static boolean findGivenElementInArrayUsingStream(int[] array, int element) {
20+
return Arrays.stream(array).filter(x -> element == x).findFirst().isPresent();
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.array;
2+
3+
import java.util.Arrays;
4+
5+
public class SumAndAverageInArray {
6+
7+
public static int findSumWithoutUsingStream(int[] array) {
8+
int sum = 0;
9+
for (int index = 0; index < array.length; index++) {
10+
sum += array[index];
11+
}
12+
return sum;
13+
}
14+
15+
public static int findSumUsingStream(int[] array) {
16+
return Arrays.stream(array).sum();
17+
}
18+
19+
public static double findAverageWithoutUsingStream(int[] array) {
20+
int sum = findSumWithoutUsingStream(array);
21+
return (double) sum / array.length;
22+
}
23+
24+
public static double findAverageUsingStream(int[] array) {
25+
return Arrays.stream(array).average().getAsDouble();
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.array;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class Find2ndLargestInArrayTest {
7+
@Test
8+
public void givenAnIntArray_thenFind2ndLargestElement() {
9+
int[] array = { 1, 3, 24, 16, 87, 20 };
10+
int expected2ndLargest = 24;
11+
12+
int actualSecondLargestElement = Find2ndLargestInArray.find2ndLargestElement(array);
13+
14+
Assert.assertEquals(expected2ndLargest, actualSecondLargestElement);
15+
}
16+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.array;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class FindElementInArrayTest {
7+
@Test
8+
public void givenAnIntArray_whenNotUsingStream_thenFindAnElement() {
9+
int[] array = { 1, 3, 4, 8, 19, 20 };
10+
int element = 19;
11+
boolean expectedResult = true;
12+
boolean actualResult = FindElementInArray.findGivenElementInArrayWithoutUsingStream(array, element);
13+
Assert.assertEquals(expectedResult, actualResult);
14+
15+
element = 78;
16+
expectedResult = false;
17+
actualResult = FindElementInArray.findGivenElementInArrayWithoutUsingStream(array, element);
18+
Assert.assertEquals(expectedResult, actualResult);
19+
}
20+
21+
@Test
22+
public void givenAnIntArray_whenUsingStream_thenFindAnElement() {
23+
int[] array = { 15, 16, 12, 18 };
24+
int element = 16;
25+
boolean expectedResult = true;
26+
boolean actualResult = FindElementInArray.findGivenElementInArrayUsingStream(array, element);
27+
Assert.assertEquals(expectedResult, actualResult);
28+
29+
element = 20;
30+
expectedResult = false;
31+
actualResult = FindElementInArray.findGivenElementInArrayUsingStream(array, element);
32+
Assert.assertEquals(expectedResult, actualResult);
33+
}
34+
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.array;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class SumAndAverageInArrayTest {
7+
@Test
8+
public void givenAnIntArray_whenNotUsingStream_thenFindSum() {
9+
int[] array = { 1, 3, 4, 8, 19, 20 };
10+
int expectedSumOfArray = 55;
11+
int actualSumOfArray = SumAndAverageInArray.findSumWithoutUsingStream(array);
12+
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
13+
}
14+
15+
@Test
16+
public void givenAnIntArray_whenUsingStream_thenFindSum() {
17+
int[] array = { 1, 3, 4, 8, 19, 20 };
18+
int expectedSumOfArray = 55;
19+
int actualSumOfArray = SumAndAverageInArray.findSumUsingStream(array);
20+
21+
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
22+
}
23+
24+
@Test
25+
public void givenAnIntArray_whenNotUsingStream_thenFindAverage() {
26+
int[] array = { 1, 3, 4, 8, 19, 20 };
27+
double expectedAvgOfArray = 9.17;
28+
double actualAvgOfArray = SumAndAverageInArray.findAverageWithoutUsingStream(array);
29+
30+
Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034);
31+
}
32+
33+
@Test
34+
public void givenAnIntArray_whenUsingStream_thenFindAverage() {
35+
int[] array = { 1, 3, 4, 8, 19, 20 };
36+
double expectedAvgOfArray = 9.17;
37+
double actualAvgOfArray = SumAndAverageInArray.findAverageUsingStream(array);
38+
39+
Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034);
40+
}
41+
}

0 commit comments

Comments
 (0)