# Different values in an Array

Page 1 of 1

## 7 Replies - 765 Views - Last Post: 06 September 2012 - 12:41 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291136&amp;s=c16450a8b1a7cf96df8ff113aa75037e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Thumbsup

Reputation: 0
• Posts: 9
• Joined: 09-March 12

# Different values in an Array

Posted 06 September 2012 - 11:43 AM

Hello,

I'm trying to make an algorithm which checks my array and returns the total amount of different values. I wrote my code, but it gives me wrong answer. Could anyone just check my code and see what I've done wrong?

```public static int differentValues(int[] array] {
int n = array.length;
int total = 0;

// This checks if array is sorted or not
for(int i = 0; i < n-1; i++) {
if(a[i] > a[i+1]) System.out.println("Array not sorted!");
}

// This should calculate the amount of total different values
for(int i = 0; i < n-1; i++) {
if(a[i] != a[i+1]) total++;
}
}
```

If my array is [1,1,2,3] I should get 3 as output, but my code gives me 2... What have I done wrong?

Edit: Both if-statements should be in same for loop, I just had to test with different values in for loop.

Is This A Good Question/Topic? 0

## Replies To: Different values in an Array

### #2 jon.kiparsky

• Beginner

Reputation: 11041
• Posts: 18,857
• Joined: 19-March 11

## Re: Different values in an Array

Posted 06 September 2012 - 12:04 PM

``` for(int i = 0; i < n-1; i++) {
```

Since n is equal to array.length, counting out to i<n-1 will only get to the penultimate item: you'll go through each item whose index is less than length-1, not including the last element whose index is length-1.

There's also a wee problem that you seem to have two different arrays. You take the length of the array provided by the method call (which you're calling "array") but you're working on some other array, called "a". If your program is compiling (which it might not be, I see another problem) then "a" must be a global somewhere. Might be the same reference as "array", might not be.

### #3 Thumbsup

Reputation: 0
• Posts: 9
• Joined: 09-March 12

## Re: Different values in an Array

Posted 06 September 2012 - 12:10 PM

jon.kiparsky, on 06 September 2012 - 12:04 PM, said:

``` for(int i = 0; i < n-1; i++) {
```

Since n is equal to array.length, counting out to i<n-1 will only get to the penultimate item: you'll go through each item whose index is less than length-1, not including the last element whose index is length-1.

There's also a wee problem that you seem to have two different arrays. You take the length of the array provided by the method call (which you're calling "array") but you're working on some other array, called "a". If your program is compiling (which it might not be, I see another problem) then "a" must be a global somewhere. Might be the same reference as "array", might not be.

I tried
``` for(int i = 0; i < n; i++)
```
but I get ArrayIndexOutOfBoundsException error. Is it because I compare a[i] and a[i+1]?

About the "wee" problem, a and array is supposed to be the same array My mistake

### #4 SwiftStriker00

• No idea why my code works

Reputation: 439
• Posts: 1,617
• Joined: 25-December 08

## Re: Different values in an Array

Posted 06 September 2012 - 12:19 PM

Thumbsup, on 06 September 2012 - 03:10 PM, said:

I tried
``` for(int i = 0; i < n; i++)
```
but I get ArrayIndexOutOfBoundsException error. Is it because I compare a[i] and a[i+1]?

Yes, because when i == n-1, then i+1 == n. Because index's are 0-indexed, the highest index will always be (n-1). So array[(n-1) + 1] will throw an error because your asking the computer to access an index in the array, that is outside of the array's possible indexes.

### #5 jon.kiparsky

• Beginner

Reputation: 11041
• Posts: 18,857
• Joined: 19-March 11

## Re: Different values in an Array

Posted 06 September 2012 - 12:19 PM

```if(a[i] != a[i+1]) total++;
```

What you're doing here is adding 1 to the total for each distinct value, that is, counting the distinct values (giving you an incorrect count, of course, because you start with your total at zero and you never add 1 for the first one). If you want to add up the distinct values, you want
```if(a[i] != a[i+1]) total+=a[i]

```

EDIT: Now, looking at this again, I'm not sure what it is you want to do. Do you want to count the number of different values in the array, or add them up?

should [1,1,2,2,3,3] give 3, or should it give 12?

This post has been edited by jon.kiparsky: 06 September 2012 - 12:34 PM

### #6 CasiOo

• D.I.C Lover

Reputation: 1576
• Posts: 3,548
• Joined: 05-April 11

## Re: Different values in an Array

Posted 06 September 2012 - 12:20 PM

Thumbsup, on 06 September 2012 - 07:10 PM, said:

jon.kiparsky, on 06 September 2012 - 12:04 PM, said:

``` for(int i = 0; i < n-1; i++) {
```

Since n is equal to array.length, counting out to i<n-1 will only get to the penultimate item: you'll go through each item whose index is less than length-1, not including the last element whose index is length-1.

There's also a wee problem that you seem to have two different arrays. You take the length of the array provided by the method call (which you're calling "array") but you're working on some other array, called "a". If your program is compiling (which it might not be, I see another problem) then "a" must be a global somewhere. Might be the same reference as "array", might not be.

I tried
``` for(int i = 0; i < n; i++)
```
but I get ArrayIndexOutOfBoundsException error. Is it because I compare a[i] and a[i+1]?

About the "wee" problem, a and array is supposed to be the same array My mistake

Yes it is because of the a[i+1]

If the length of the array is 10 then:
n = 10
i = 1, 2, 3 ..., 9
i+1 = 10 which would be out of bounds of the array

### #7 baavgai

• Dreaming Coder

Reputation: 7164
• Posts: 14,931
• Joined: 16-October 07

## Re: Different values in an Array

Posted 06 September 2012 - 12:31 PM

Hmm....
```[1,1,2,3]
i=0, (1!=1)==false, total=0
i=1, (1!=2)==true, total=1
i=2, (2!=3)==true, total=2

```

Of course, having starting number the same is your edge case.

While less neat, you could do something like:
```int total = 1, last=a[0];
for(int i = 1; i < n; i++) {
if(a[i] != last) { total++; last = a[i]; }
}

```

### #8 Thumbsup

Reputation: 0
• Posts: 9
• Joined: 09-March 12

## Re: Different values in an Array

Posted 06 September 2012 - 12:41 PM

Thanks guys, I didn't notice that I never counted the first one, that's why I always got one less that the answer. Also thanks for the explanation, just the mathematical failure from my side

```int total = 1;

if(array.length < 1) return 0;

for(int i = 0; i < array.length - 1; i++) if(array[i] != array[i+1]) total++;
```

This worked!