# Number of occurrences in a int[ ]

• 10-10-2009, 08:02 PM
random0munky
Number of occurrences in a int[ ]
I'm having a little trouble as to taking two int []'s and if there are two elements that are the same, skipping over them. In the end I need the program to print out the elements that don't have any duplicates at all. For instance:
int [] a = {4, 5, 5}
int [] b = {6, 6, 3}

The program will output an array which contains,
int [] output = {3, 4}

Here's all the code I have
import java.util.Arrays;
Code:

class Test2{
public static void main(String args[]){
int arr[] = new int[]{2, 5, 1, 1, 2, 3}; // 1, 1, 2, 2, 3, 5
Arrays.sort(arr);
//System.out.print(Arrays.toString(arr)) ;

int newArray[] = new int[arr.length];
int temp = 0 ;
int count = 0 ;

for(int i = 0; i < arr.length; i++){
temp = arr[i];
//System.out.print(temp + " ") ; // 1, 1, 2, 2, 3, 5
int val = i + 1 ;
//System.out.print(val + " ") ; // 1, 2, 3, 4, 5, 6
if(val < arr.length){
if(temp == arr[val])
continue;
}
newArray[count++] = temp;
//System.out.print(temp + " ") ; // 1, 2, 3, 5
}

for(int i = 0; i < newArray.length; i++){
System.out.print(newArray[i] + " ");
}
}
}

The output = 1 2 3 5 0 0

Please ignore all of the System.out.println's in the code. I was doing checks to try and narrow down the problem.

Thank You I appreciate any input.
• 10-10-2009, 08:22 PM
CodesAway
OK, I'm confused since your question concerns two arrays, but your code only has one. So, let me see if I understand the goal. Given two arrays, you want to return an array that contains only the elements that appear once.
Now, it's not clear from your example if a value appeared once in each array if it should be included in your results, so I'm not sure how you want to handle that.

Second, what is it you want from us? Your code already does what you need, or do you want to use a different method?

To clarify, your code first sorts the array, then it compares adjacent members. If the next value is identical to the current one, you skip it. If the next value is different, you store the current value in the resulting array.

Is your question how to extend this to two arrays?

• 10-10-2009, 08:26 PM
random0munky
Quote:

Originally Posted by CodesAway
OK, I'm confused since your question concerns two arrays, but your code only has one. So, let me see if I understand the goal. Given two arrays, you want to return an array that contains only the elements that appear once.
Now, it's not clear from your example if a value appeared once in each array if it should be included in your results, so I'm not sure how you want to handle that.

Second, what is it you want from us? Your code already does what you need, or do you want to use a different method?

To clarify, your code first sorts the array, then it compares adjacent members. If the next value is identical to the current one, you skip it. If the next value is different, you store the current value in the resulting array.

Is your question how to extend this to two arrays?

CodesAway: So, let me see if I understand the goal. Given two arrays, you want to return an array that contains only the elements that appear once.

This is what I want the program to do.

In the output it prints 1, 2, 3, 5 where it should only be outputting 3, 5 and thats it sorry for the misunderstanding
• 10-10-2009, 08:36 PM
CodesAway
Oh, ok, got it. Well, in your code, you only skip the value if the next one is the same. This means, if there are multiple such values, the last one is stored (since the next value is different).

Instead, what you want to do is "if the next element is the equal to the current one, skip all such equal elements. Otherwise, add it to your new array", right?

Hopefully the above "psudocode" points you in the right direction.
• 10-10-2009, 08:43 PM
random0munky
Quote:

Originally Posted by CodesAway
Oh, ok, got it. Well, in your code, you only skip the value if the next one is the same. This means, if there are multiple such values, the last one is stored (since the next value is different).

Instead, what you want to do is "if the next element is the equal to the current one, skip all such equal elements. Otherwise, add it to your new array", right?

Hopefully the above "psudocode" points you in the right direction.

I understand your psudocode, however just to clarify,
Code:

if(temp == arr[val])
continue;

Is the problem child and I should not have the continue statement...
• 10-10-2009, 09:10 PM
CodesAway
Quote:

Originally Posted by random0munky
I understand your psudocode, however just to clarify,
Code:

if(temp == arr[val])
continue;

Is the problem child and I should not have the continue statement...

I'm not sure how to answer that question. You need a way to skip past ALL the occurrences of equal values, instead of just 1, like the above statement does. By skipping only one, the last occurrence will be stored. If you skip all the occurrences, none will be stored.

Then, you need to handle the case when there is only one occurrence, since you only want to store the value if it occurs once.
• 10-10-2009, 09:27 PM
random0munky
I apologize for being so new to programming but I understand what you are telling me, but I just can't figure out how to put it in code form. I know when

if(temp == arr[val])

I did a println for temp and val and it prints out

temp: 1, 1, 2, 2, 3
val: 1, 2, 3, 4, 5

I'm still not quite wrapping my head around how to just skip over what you said and just print the 3 and the 5.
• 10-10-2009, 09:38 PM
CodesAway
Quote:

Originally Posted by random0munky
I apologize for being so new to programming but I understand what you are telling me, but I just can't figure out how to put it in code form. I know when

if(temp == arr[val])

I did a println for temp and val and it prints out

temp: 1, 1, 2, 2, 3
val: 1, 2, 3, 4, 5

I'm still not quite wrapping my head around how to just skip over what you said and just print the 3 and the 5.

Ok, let's begin by breaking down the "psudocode" I gave, "if the next element is the equal to the current one, skip all such equal elements. Otherwise, add it to your new array".

I took your original code, refactored it a little, and added in my above psudocode as comments. Your task is to write code to "skip all such equal elements".

Code:

import java.util.Arrays;

class Test2
{
public static void main(String args[])
{
int arr[] = new int[] { 2, 5, 1, 1, 2, 3 }; // 1, 1, 2, 2, 3, 5
Arrays.sort(arr);
// System.out.print(Arrays.toString(arr)) ;

int newArray[] = new int[arr.length];
int count = 0;

for (int i = 0; i < arr.length; i++) {
// if the next element is the equal to the current one
if (i + 1 < arr.length && arr[i + 1] == arr[i]) {
// skip all such equal elements
} else {
newArray[count++] = arr[i];
}
// System.out.print(temp + " ") ; // 1, 2, 3, 5
}

for (int i = 0; i < newArray.length; i++) {
System.out.print(newArray[i] + " ");
}
}
}

• 10-10-2009, 09:49 PM
random0munky
Yay!!! oh My Goodness. Your are absolutely getting some rep from me. Thank You oh so much. Also the cleaner looking code is much easier to read then what I first started. Now I just need to take out the 0's in the empty array elements.
• 10-10-2009, 09:53 PM
CodesAway
I take it you got it working, I'm glad.

If you have any further questions, don't hesitate to ask.
• 10-10-2009, 10:22 PM
random0munky
Yuppers I sure did. I deff. will post questions when the need arises.