• Post Reply Bookmark Topic Watch Topic
  • New Topic

Eliminating duplicates in an array  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble figuring out the code that belongs under the method for this exercise.

Here's the exercise in full: "Write a method that returns a new array by eliminating the duplicate values in the array using the following method header:

   public static int[] eliminateDuplicates(int[] list)

Write a test program that read in ten integers, invokes the method and displays the result.

Here is what I have so far. I apologize, I'm still a beginner so I may not post it or format it correctly.

import java.util.Scanner;
public class DuplicationElimination {


If anyone can give me a hint or tips or a nudge in the right direction I would appreciate it. As I said, I'm also really new and we haven't learned things like hashset or anything.
 
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a hint or tips or a nudge in the right direction 

I'd start by making a list of the steps that the program needs to take to solve the problem.  Work out the logic before trying to write the code.

Some considerations:
How to detect a duplicate.
what to do when one is found.
If the array being returned is smaller than the one received, when and how will the new array be created.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also do the items in the new array need to be in the same order as the input array?
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Norman said, you have to work out an algorithm before trying to write code.

Sometimes it helps to imagine the problem in different terms, too. Say I give you a box of ten balls of different colors but some balls have the same color. Then I asked you to give me back another box with balls that had all the same colors as the ones in the first box but this time, the balls have be all different colors; no two balls should have the same color in the box you give back to me. How would you do it?

Or if you prefer, you can imagine this with a box of a dozen donuts, where you might have a few of each kind (glazed, chocolate, creme-filled, etc). You change your mind and say, no, I only want one of a kind, so you get another box that you'll transfer some of the donuts from the first box to. The second box should only contain one donut of each kind when you're done.

Whatever your process in doing any of these two tasks, that's basically the same process you need for your program.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and Welcome to the Ranch!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The examples I gave before may or may not lead you to an appropriate algorithm for your problem so I'll give you another example that is more likely to do so. Say you were asked to take a survey of all the workers in a department. You want to know how many different job titles workers in that department had. Given a list of workers and their corresponding job titles, how would you come up with a list of all the job titles in that department? Obviously, the list you'd come up with would not have any duplicate job titles, even if two or more employees in the department had the same title. You might think about sorting the list by job title first to make your job easier but try to think of how you'd do it if the list of workers you were given was sorted by name and not by job title.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The second box should only contain one donut of each kind when you're done.


This is one interpretation of the "eliminate duplicates" requirement, and chances are that it is what the requirement actually means in your case. But there is another possible interpretation...

Suppose you have the array {1, 1, 1, 2, 3, 3}. Then each of the 1's is a duplicate, because there are other 1's in the array. Likewise both of the 3's are duplicates. So the result after eliminating the duplicates should be {2}.

If I had been the one giving out this assignment, expecting a program to return {1, 2, 3} given that array, I would be forced to admit that my requirements were ambiguous and that {2} was also a correct result.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul makes a good point.

An analogy to Paul's interpretation would be something like this: You want to go into a school and survey how many kids were single births and which ones were multiple births (twins, triplets, quadruplets, etc.). For one particular statistic, you want to only consider kids who were single births. Think of how you'd come up with that list if the school was known to have an unusually large population of kids who were multiple births?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!