aspose file tools*
The moose likes Beginning Java and the fly likes Remove duplicates in an Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Remove duplicates in an Arrays" Watch "Remove duplicates in an Arrays" New topic
Author

Remove duplicates in an Arrays

Mel Ram
Greenhorn

Joined: Sep 30, 2009
Posts: 12
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....

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Note that after you sort an array, duplicate ints will be adjacent to each other. Does that help you?


[Jess in Action][AskingGoodQuestions]
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Ernest Friedman-Hill wrote:Note that after you sort an array, duplicate ints will be adjacent to each other. Does that help you?


Very good point, I think that is the clue ;)


Other (different) thing in your program, what would happen if I enter a '%' or a 'b' in the input?

And a very small point, you have a final var NUMBERS_IN_SET, I think you can use it in more places in your code ;)

Keep us informed of your progress!

Vidmantas Maskoliunas
Greenhorn

Joined: Nov 16, 2009
Posts: 22
Another interesting approach is to use java.util.TreeSet<Integer> API. But that may be an overhead.


SCJP 6.0, willing to find Java job in NZ/AU and move there - LinkedIn profile - Java blog
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
He also can compare the toString of both (very bad practice never do that) but he has to learn how to use arrays, not only get the result
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
why dont put the array in a set and put the set into the array again? so your duplicates will be removed macigally;)
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Mel Ram wrote: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.


Linear search means you scan the arrays.

You don't need to sort the arrays. The trick is to use nested loops (loops inside other loops).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
The nested loop technique runs in quadratic time, so it is not very efficient, but is almost certainly what your teacher wants you to find.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Campbell Ritchie wrote:The nested loop technique runs in quadratic time, so it is not very efficient, but is almost certainly what your teacher wants you to find.


If performance is a concern the inner loop linear searches can later be replaced with hashed searches which will give linear complexity to the algorithm.

Sorting is only a good option if the arrays are intially sorted, not if you first need to sort them.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Vidmantas Maskoliunas wrote:Another interesting approach is to use java.util.TreeSet<Integer> API. But that may be an overhead.


There's no reason to use a TreeSet when order doesn't matter and there's a HashSet available.
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Embla Tingeling wrote:
Vidmantas Maskoliunas wrote:Another interesting approach is to use java.util.TreeSet<Integer> API. But that may be an overhead.


There's no reason to use a TreeSet when order doesn't matter and there's a HashSet available.


Furthermore, there's no reason to use either a TreeSet or a HashSet when the target is to learn how Arrays work :P
Rohit Aggarawal
Ranch Hand

Joined: Jun 26, 2009
Posts: 39
Write you own comparator and then trying comparing both the arrays.


SCJP 6.0
SCBCD for JEE 5

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39388
    
  28
I think, Embla, that the intention of the exercise is to write something simple and forget about performance.
Mel Ram
Greenhorn

Joined: Sep 30, 2009
Posts: 12
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!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11401
    
  16

Loops don't return values...methods do. That's not to say you couldn't have a loop inside a method, or a loop that sets a value that then gets passed somewhere...

To have a method return something, you have to declare what it will return - a String, an int, a boolean... it can be whatever you want. then you literally return that thing:



then, when you call the method, you need something to store what it returns (usually, although not required):

int dayValue = myMethod();

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

I sometimes overlook performance so bear with me:

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.

My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

or use a set instead of an array list...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove duplicates in an Arrays