# Filtering an array

• 10-15-2010, 05:43 PM
counterfox
Filtering an array
Im trying to make a method to filter an array

say array1 has these contents 10 20 30

and array2 has 11 30

the array values are entered by the user so both arrays can be any length

The method would take in these two arrays then return a new array, which has the contents of array1 plus any new numbers from array2. So the returned array would be 10 20 30 11

I can write the main method to take an array of any length from the user but im having trouble actually filtering the array.

My idea was to get array2[i] from a for loop and test this against each content of array1 then move on to array[i+1]

how ever I'm having trouble implementing this. Is there anyone that could help with the way to make this method?

• 10-15-2010, 05:47 PM
JosAH
Do you have to use arrays? If you'd use a few collections (Sets) the code would be just a few lines.

kind regards,

Jos
• 10-15-2010, 05:50 PM
counterfox
yeahh its like a homework sheet thing for university about arrays and this is the only question i'm stuck on :(
• 10-15-2010, 05:53 PM
JosAH
Quote:

Originally Posted by counterfox
yeahh its like a homework sheet thing for university about arrays and this is the only question i'm stuck on :(

Ok, that's a fact then ;-) Can any of the arrays contain duplicate values? i.e. A = { 1, 2, 2, 3 }, B = { 3, 3, 4, 5, 6 }, result = { 1, 2, 3, 4, 5, 6 }?

kind regards,

Jos
• 10-15-2010, 05:59 PM
counterfox
Yes the arrays can contain duplicate values, however when the new array to returned is made it must start with the contents of the first array then any new values from the second array are added onto the new array so the result here would be:

1 2 2 3 4 5 6

Regards,

James
• 10-15-2010, 07:52 PM
JosAH
Quote:

Originally Posted by counterfox
Yes the arrays can contain duplicate values, however when the new array to returned is made it must start with the contents of the first array then any new values from the second array are added onto the new array so the result here would be:

1 2 2 3 4 5 6

Ok, so given arrays A and B you can copy all the elements of A to the result while you have to check every element of B whether or not it is present in A. If it is don't do anything, if it isn't copy it to the result. Note that you don't know the size of the result array in advance, all you know that it is less or equal to the sizes of arrays A and B.

kind regards,

Jos
• 10-15-2010, 08:23 PM
counterfox
Thanks,

One thing im confused with is for example as we don't know the sizes of either array until after the user has entered it I found its hard to calculate in the method first how to get the size of the new array.. Heres what I thought

1.) I should find out how big the new array should be.

So what I thought was to see how many new numbers (from array B) would be in the result array.

From this i made a local variable which is a counter = 0; then in a for loop had a test against a.length
//i is the variable in first for loop, j is the variable in the second for loop
Then made nested loop in this where now i could test if a[i] != b[j] then do something like increment the counter as a new number would be in the new array.

So this would test a[0] against b[0] then b[i] etc until the second for loop is finished. This is what I thought. However I could not get this to work or don't know if im missing something basic or overcomplicating things.

2.) I would now create the new array
int resultArr[] = new int [a.length+counter];

Then from this fill out the array with array A then do another similar test like the 1st step to fill in the result array and return it.

This was my initial idea but im not sure if its overcomplicating a more easy solution but I cant implement it and its driving me mad.

Regards,

James
• 01-19-2012, 04:47 AM
diamonddragon
Re: Filtering an array
Maybe this help...
array1 also can be of any length, same as array2, and this example is with fixed number of elements.
Code:

```public class FilteringArray {     public static void main(String[] args) {         //create array1         int[] array1 = {1, 3, 2, 7, 5, 3, 2, 7, 4};         //create array2 of length between 1 and 10         int[] array2 = new int[(int)(1 + Math.random() * 10)];         //fill array2 with random numbers between 1 and 10         for (int i = 0; i < array2.length; i++)             array2[i] = 1 + (int)(Math.random() * 10);                                                    //create array of array1 and array2         int[] array = new int[array1.length + array2.length];         System.arraycopy(array1, 0, array, 0, array1.length);         System.arraycopy(array2, 0, array, array1.length, array2.length);                 int[] finalArray = filterArray(array);                         //print arrays         System.out.println("array1:");         for (int i = 0; i < array1.length; i++)             System.out.print(array1[i] + " ");         System.out.println();                 System.out.println("array2:");         for (int i = 0; i < array2.length; i++)             System.out.print(array2[i] + " ");         System.out.println();         System.out.println("array1 + array2:");         for (int i = 0; i < array.length; i++)             System.out.print(array[i] + " ");                        System.out.println();                         System.out.println("Array result:");         for (int i = 0; i < finalArray.length; i++)             System.out.print(finalArray[i] + " ");                        System.out.println();     }         public static int[] filterArray(int[] a) {         //count different         int count = a.length;//suppose all different         start:         for (int i = 0; i < a.length; i++)             for (int j = i + 1; j < a.length; j++)                 if (a[i] == a[j]) {                     count--;                     continue start;                                    }                                                                    //create filtered array         int[] filteredArray = new int[count];            count = 0;         start:         for (int i = 0; i < a.length; i++) {             for (int j = i + 1; j < a.length; j++)                 if (a[i] == a[j])                      continue start;              filteredArray[count] = a[i];         count++;         }         return filteredArray;            }             }```