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 should read it and print it out as [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!
For now though, the first thing I need to know is to how I can eliminate duplicates. I think im suppose to use the linear search method.
I just don't know how to use the linear search method. The example in my homework is not very good in terms of how to use it. I'm so lost just in this part alone for the past 2 days now.
lol... sorry if im rambling. Im just stressed. Here's my code (which is a workable program btw! Thanks!
UPDATE: Shoot... I don't think It's the linear function i need to use to find duplicates....
Write you own comparator and then trying comparing both the arrays.
SCBCD for JEE 5
Joined: Oct 13, 2005
I think, Embla, that the intention of the exercise is to write something simple and forget about performance.
Joined: Sep 30, 2009
Ernest Friedman-Hill wrote:Note that after you sort an array, duplicate ints will be adjacent to each other. Does that help you?
Ha! That's funny... I went to the teacher and said the exact same thing!! The reason I sorted the array was because I was following the advice of another classmate that mentioned that it's easier to remove if they're all lined up. But in fact, no.
So i spoke to my teacher and there's something he really helped me with. Yes, the idea is to create a loop where it returns true, and then have a method that invokes it if its true. Also, this loops is suppose to read each individual integer from one list to the other and when it finds one, set a count for it. However we couldn't get much into it but I'm a little slow at how to do this. I've never done methods, let alone loops that return a value. Any suggestions to go about it?
BTW, thanks everyone for all contributing in this! You guys are awesome!
1. Create a collection c (maybe an array list)
2. Add all elements of array 1 to c (check contains(x) before adding)
3. Add all elements of array 2 to c (check contains(x) before adding)
4. Convert c into an array
5. Voila ! You now have an array that does not contain duplicates.
Advantages: Simple to implement
Possible performance issues:
1. including a collection
2. listening to salvin
3. using the contains method for every iteration
4. converting an collection into array.