• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparing Array Elements

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am new to the forum and quite new to java. I appreciate all suggestions, feedback, or help in advance. Thanks.

The problems asks to compare two sorted arrays of integers, returning the number of values that are the same between the two. If there are duplicates, the array with the least amount will be counted. For example:

list1: (1, 2, 3, 3, 3, 3, 3, 5, 6, 7)
list2: (1, 1, 3, 3, 5, 9, 9)

numSame(list1, list2);

would return a total of 4 (1 once, 3 twice, and 5 once).

I am not allowed to use additional arrays or another structured object like an ArrayList. I'm not allowed to modify the arrays that are passed as parameters.

Here is what I have so far:



I am stuck right now on where to go from here. Any tip or guidance would be greatly appreciated. Thank you.

-Brandon
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Pencil and paper, and a large eraser. That's what you need.

Write down in very simple terms how you intend to do this counting; that is called pseudocode. When you have got the pseudocode and you are sure it will work, then creating code will be very easy.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch!

Can you see where your current code is falling short? What should it be doing that it is not doing now?

Hint: It is finding that "1" is in both arrays, but it is missing "3". Do you see why?
 
Brandon Peeples
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the welcome. I'll try your recommendation and see if I can improve.
 
Brandon Peeples
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the welcome Marc. Yes, I can see why it does that. The method is only comparing the values of both arrays at the same element. I need to find a way to compare the element of one array with all the other elements of the second array. I'm reading through my book to see if I missed anything. Thanks for the guidance.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brandon Peeples wrote:... I need to find a way to compare the element of one array with all the other elements of the second array...


That's exactly right! As Campbell said above, "Write down in very simple terms how you intend to do this counting..." It looks like you're well on your way.

Can you make your description even more specific? For example, get the first element from the first array, compare it to the first element of the second array, then compare it to the second element of the second arrary, then ... , unitl ___. This process will help you understand the logic and get you closer to what the code should look like.

 
Brandon Peeples
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow you are so awesome Marc. I think I improved one more step. At least I hope so haha.

The problem now is getting rid of the duplicates. Once again I will rely on the trusty pencil, paper, eraser combo . Thanks again.
 
Brandon Peeples
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created another solution. I tried to change the value of j but i don't think it worked out right. Here is what I have at the moment.



I'm trying to change the value of "j" when the if statement is true so it won't duplicate next time, but it isn't working out right.

-Brandon

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just to throw a monkey wrench in, are you guaranteed that the arrays will be sorted? that can make a difference in how you approach it...
 
Ranch Hand
Posts: 56
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice assignment, at first it looks simple but it's turns out to be more complicated when you try to find the solution. If you (OP) have the solution do post it. I think I've got a nice solution but I'm curious to see how others would approach it.

By the way, if you're looping through an array you can use a enhanced for which can come in handy here I think.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
 
Brandon Peeples
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@fred Yes, they are always sorted.

@Misha The enhanced for is pretty awesome. Thanks. Unfortunately I can't use that since we haven't learned that in class. I appreciate the link though. I haven't figured a solution yet.
 
Misha van Tol
Ranch Hand
Posts: 56
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A normal for-loop will do the job too. Before starting to program you already need to know the solution of the problem. They probably teach you to make a flow-chart first. But you can also use normal sentences to work with the problem like this:

- loop through list1 and for each element in it:
- loop through list 2 and compare each element with the element from list1
- if they're equal increase a counter, stop 2nd loop and know that you don't have to check this element from list2 anymore
(nor the previous elements).

Good luck!
 
Brandon Peeples
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I created the right solution. Here is the final code:



What I did different was change the value of "j" if the elements matched so it wouldn't match again. I also added a break to stop the loop. I am a super newbie to programming. I appreciate all the advice and guidance. It was great to finally finish the problem. Thanks guys. You are all great
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what if the value in the array are like this..
int[] list1 = {1, 10000, 1, 1, 2, 2, 3, 4, 5, 8, 12};
int[] list2 = {1, 1, 3, 3, 3, 3, 6, 6, 7, 7, 8, 9, 10, 11};

 
Brandon Peeples
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point haha. Maybe this is better. I hope...

list2[j] -= list2[j]; instead of list[j] += 9999;

Thanks for the recommendation of removing that weird 9999.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is zero is there???

what if the value in the array are like this..
int[] list1 = {1, 10000, 0, 1, 2, 2, 3, 4, 5, 8, 12};
int[] list2 = {1, 1, 3, 3, 3, 3, 6, 6, 7, 7, 8, 9, 10, 11};


i this you should find another logic !!
 
Brandon Peeples
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmm... What about converting the element to a double? Since all of the numbers in the array have to be type int.

list2[j] *= 0.00;
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i am not complete sure of the actual problem statement...
and about your solution ... you cannot write a double in to a int array !!!
 
Misha van Tol
Ranch Hand
Posts: 56
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Tharakan wrote:what if the value in the array are like this..
int[] list1 = {1, 10000, 1, 1, 2, 2, 3, 4, 5, 8, 12};
int[] list2 = {1, 1, 3, 3, 3, 3, 6, 6, 7, 7, 8, 9, 10, 11};
...
what if the value in the array are like this..
int[] list1 = {1, 10000, 0, 1, 2, 2, 3, 4, 5, 8, 12};
int[] list2 = {1, 1, 3, 3, 3, 3, 6, 6, 7, 7, 8, 9, 10, 11};



Since the array's are always sorted the order would be different. Which doesn't mean using an arbitrary high value is the right approach. Just think over what you are doing here, you don't need to check the value in list2 anymore so you set it to '9999'. When continuing with the program you keep comparing items from list1 with all those '9999' values when you already know they don't match. Why bother comparing them?
 
Brandon Peeples
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll give it a try. Thanks for the suggestion! I tried to think of a way to make it not check an element again if it matched, but I couldn't think of anything. I found another way to make it work though. Here is my updated code:



I tried to take advantage of knowing that the array lists are from least to greatest. If any element values matched, the element value from the second list would decrease the first match by 1. Then all other matching elements would get the decreased first match value. Therefore, duplicates cannot be matched because it is from least to greatest.
 
reply
    Bookmark Topic Watch Topic
  • New Topic