public class LoopTest {
public static void main(String[] args) {
int[] myarr = {12, 12, 12, 8, 15, 15};
//Boolean array to mark the elements,defaults false
boolean[] b = new boolean[myarr.length];
//Compare Consecutive values and mark them true if equal
for (int i = 1; i < myarr.length; i++) {
if (myarr[i - 1] == myarr[i]) {
b[i - 1] = b[i] = true;
}
}
int sum = 0;
//Add all the values in myarr with indices marked as equal
for (int i = 0; i < b.length; i++) {
if (b[i]) {
sum += myarr[i];
}
}
System.out.println(sum);
}
}
Output:
66
Explanation:
12+12+12+15+15
Is there a better/cleaner way to compare values in array and add only values if they are equal, without using utility methods?
You could keep a running count of duplicate items and add them to your sum
when the run ends.
int[] myarr = {12, 12, 12, 8, 15, 15};
// assumes > 0 length
int count = 1;
int sum = 0;
for (int i = 1; i < myarr.length; i++) {
if (myarr[i] == myarr[i - 1]) {
count++;
} else {
if (count > 1) {
sum += count * myarr[i - 1];
}
count = 1;
}
}
// handle if last elements are duplicates
if (count > 1) {
sum += count * myarr[myarr.length - 1];
}
System.out.println(sum);
The following code will work:
public class LoopTest {
public static void main(String[] args) {
int[] myarr = {12, 12, 12, 8, 15, 15};
int sum = 0;
int occ = 1;
for (int i = 1; i < myarr.length; i++) {
if (myarr[i - 1] == myarr[i]) {
occ++;
} else {
if (occ > 1) {
sum += (occ * myarr[i - 1]);
}
occ = 1;
}
if (i == myarr.length - 1) {
if (occ > 1) {
sum += (occ * myarr[i - 1]);
}
}
}
System.out.println(sum);
}
}
You can solve this with linear efficiency
. This program is a little bit cleaner and works in all conditions, checking all the edge cases . It results in the correct answer of 66 for your problem. It loops through the array, and checks if each element is consecutive (same as previous element). If so, it adds the element's value onto the sum. Edge cases need to be included to account for the starting elements of each consecutive block, which have to be added to the sum as well.
private static int consecutiveCompare(int[] array)
{
int sum = 0;
for (int i = 1; i < array.length; i++)
{
if (array[i] == array[i-1])
{
if (i == 1)
{
sum += array[i];
}
else if (array[i] != array[i-2])
{
sum += array[i];
}
sum += array[i];
}
}
return sum;
}
Haven't tested all edge cases, here is what I have in my mind:
public class LoopTest {
public static void main(String[] args) {
int[] myarr = {1,1,1,2,2,3,3,4};
//Boolean array to mark the elements,defaults false
boolean[] b = new boolean[myarr.length];
//Last value tracker.
int lastVal = myarr[0];
//Count occurrences in a sequence.
int cntr = 1;
//Sum counter.
int sum = 0;
//Compare Consecutive values and mark them true if equal
for (int i = 1; i < myarr.length; i++) {
if (myarr[i] == lastVal) {
cntr++;
//If last sequence mathching.
if (i == myarr.length-1) {
sum += lastVal * cntr;
}
} else {
if (cntr > 1) {
sum += lastVal * cntr;
//Reset counter.
cntr = 1;
}
lastVal = myarr[i];
}
}
System.out.println(sum);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.