# Thread: HELP!!! Problems with Array: matching, sorting, etc

1. Member
Join Date
Sep 2009
Posts
16
Rep Power
0

## HELP!!! Problems with Array: matching, sorting, etc

So here's my dilemma. I am suppose to have 2 sets of arrays that the user inputs. The program will have to sort through the arrays and check for duplicate integers. For example, if an array has a set [2,3,4,4,5] , it is equivalent to [2,3,4,5]. THEN, it tries to match with the second set of integers and check whether they're the same [2,3,4] [4,3,2] (order does not matter) ; different [2,3,4] [5,6,7] . There's more combinations but I am trying to go at it one by one. If you guys help me out here I would gladly appreciate it!

Java Code:
```import java.util.Scanner;
import java.util.Arrays;

public class MatchingIntegers {
public static void main(String[] args) {

final int NUMBERS_IN_SET = 10;
int[] numbers1 = new int[NUMBERS_IN_SET];
int[] numbers2 = new int[NUMBERS_IN_SET];

Scanner input = new Scanner(System.in);

System.out.println("Enter the first set of numbers individually (#1 - 10)\n");

// Prompt user to input the first set of numbers
for (int i = 0; i < numbers1.length; i++) {
System.out.print("Enter a number: ");

// Convert string into integer
numbers1[i] = input.nextInt();
}

System.out.println("Enter the second set of numbers individually (#1 - 10).\n");

// Prompt user to input the second set of numbers
for (int i = 0; i < numbers2.length; i++) {
System.out.print("Enter a number: ");

// Convert string into integer
numbers2[i] = input.nextInt();
}

// Sort the whole arrays for both sets
java.util.Arrays.sort(numbers1);
java.util.Arrays.sort(numbers2);

// perform a linear search on the data

// Prepare the result
String output = " The first array is : ";
for (int i = 0; i < numbers1.length ; i++) {
output += numbers1[i] + " ";
}

// Display the result
System.out.println(output);

output = "The second array is : ";
for (int i = 0; i < numbers2.length ; i++) {
output += numbers2[i] + " ";
}

// Display the result
System.out.println(output);

}
}```
Last edited by jrelvi23; 11-19-2009 at 12:22 AM.

2. So, what do you need help with, specifically? - it's not obvious from your post.

3. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
I need help on how I could look for duplicate numbers in each array and then how can I compare them to each other. Are both arrays the same? are they different?

4. For all of them, I would say sort the array first - it makes the job MUCH easier.

Then, look at a sorted listed, like the example you gave "[2,3,4,4,5]" and notice what happens to the duplicates.

For the second question, it seems that order and duplicates don't matter - thus, you are describing a Set. Again, sorting will make this much easier. Compare two sorted sets and see what you notice. How can you tell if the sets are same when there are no duplicates? When there are duplicates?

5. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
I found the way to sort them out... Check the new code. I added two lines above //Display the results. Now, it prints out the array in ascending order which is nice. As for the duplicates, it just lines them up next to each other... Not sure if that is what you meant by "notice what happens..."

I need help on how to compare them to each other. After the duplicates are ignored, it prints out the new array without the duplicates. See if they are the same array, then it suppose to print, "Both arrays are identical." If they're not... you get the picture. This is a part that makes me go "AAHHH!!" and I'm a sure it's probably a small code too which aggravates me!

Thanks btw!

6. Exactly, the duplicates line up in a sorted array, which makes it very easy to detect (and remove) them.

As for the second part, are you suppose to check if two different arrays are equal, or if the array (with duplicates) is equal to the same array without duplicates?

7. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
How do you remove the duplicates exactly? I am suppose to check if the two different arrays are equal AFTER the duplicates have been removed.

8. You don't have to remove the duplicates to determine if the arrays are equal without duplicates.

That being said, if you want to remove the duplicates, the easiest way would be to add the unique items to a List, and then convert the list to an array. Are you allowed to use a list? You can do it with only arrays, if that's required.

9. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
we could use a list. coud i see both ways though? for future reference.

10. Well, how would you do it? If you had a list of values that are sorted, how could you obtain a list of unique values?

For example, given the values "[2,3,4,4,5]", how would you turn it into "[2,3,4,5]" (removing the duplicate 4)? It's usually best to describe the steps first, and once you understand them, then write code that performs those same steps.

11. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
lol... I'm sure you gave me a hint there for unique values but I'm still not sure....

Am I suppose to count the occurences of each letter and if there's more than 1, do not allow it to print again...

there's a section in the textbook that looks like it might point me to the right direction using the linear search approach. But the problem is that it compiles correctly, but then I get an error that says:

Here's the code from the book.

Java Code:
```public class LinearSearcher {

public static int linearSearch(int[] list, int key) {
for (int i = 0; i < list.length; i++) {
if (key == list[i])
return i;

}
return -1;
}
}```
This is the error code: ----jGRASP exec: java LinearSearcher

java.lang.NoSuchMethodError: main
Exception in thread "main"
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Is this even helpful to me to try to figure out?
Last edited by jrelvi23; 11-18-2009 at 09:15 PM.

12. That last snippet you posted just checks if key is in list and returns -1 if it is. It wont work because you're missing the main method that calls it, populates the list, picks the key etc

13. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
Thanks for that! I am pretty sure I need to use a linear search method. Being new to java... I am not sure how to use it into my code....

14. linear search just checks every item in the list until it finds the item or runs out of things to check. just a basic loop can cover it.

15. Member
Join Date
Sep 2009
Posts
16
Rep Power
0
Ok well thanks guys. I'll try to figure it out and let you guys know.

16. you cant run linear searcher you have to instiate that class like you would a java class inside the class that has your MAIN METHOD

Java Code:
```
package arrays;  //you need to have a folder called arrays and every class
//you create has to start with this package arrays line
//compile each .java separately
//then run the .class that has the main in it
//in this case my entry point is DO_StUFF
public class DO_STUFF
{

DO_STUFF(){}// whatever the class needs to do

public static void main(String args[])
{ int [] myArray = new int [4];
for(i= 0; i<4; i++)
myArray[i] = (i + 1);
LinearSearcher L = new LinearSearcher();
L.linearsearch(myArray, 7);
}
}```
Last edited by aaroncarpet; 11-19-2009 at 12:19 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•