File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove duplicates in an Arrays

 
Mel Ram
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that after you sort an array, duplicate ints will be adjacent to each other. Does that help you?
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another interesting approach is to use java.util.TreeSet<Integer> API. But that may be an overhead.
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write you own comparator and then trying comparing both the arrays.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, Embla, that the intention of the exercise is to write something simple and forget about performance.
 
Mel Ram
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
salvin francis
Bartender
Pie
Posts: 1230
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
salvin francis
Bartender
Pie
Posts: 1230
10
Eclipse IDE Google Web Toolkit Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or use a set instead of an array list...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic