• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating an array from elements in another array  RSS feed

 
Zack Virgo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I'm currently a student learning Java and I've been hitting my head over and over trying to figure this one question out.

We were given an array and asked to create a new array containing only the unique elements from the given array. Now I've read our chapters on array's and gone over our slides over and over and am coming up empty handed. Iv'e also been googling it and have found lots of ways if I wanted to use HashSets, which I can't use, or am inputting from a scanner, which I'm not. So far this is the best I'v come up with:




And that just prints the full array as opposed to just the unique elements. I am at the literal end of my rope and have been staring at this damn thing for days. Any help nudging me in the right direction would be very appreciated.

Thank you.
 
Carey Brown
Saloon Keeper
Posts: 3254
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two ways:

You could first sort the array and then loop through it and if an entry is the same as the previous entry it is not unique.

You could have two nested loops, one to visit each element, and one to visit counting each element that has the same value as the one visited by the first loop. (Brut force, ugly, inefficient.)
 
Carey Brown
Saloon Keeper
Posts: 3254
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zack Virgo wrote:
You are close. Change your if() to skip over cases where i == j.
Also the value of isDistinct is backwards to what the name of the variable describes.
 
Zack Virgo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright I've been looking at this with your suggestions.

I flipped the values of my isDistinct and I think that also helped woth the skipping of i==j by making isDistinct=false if i==j? Its now only giving me the numbers in the sequence that are not unique, so I must have something backwards somewhere.

 
Carey Brown
Saloon Keeper
Posts: 3254
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You are close. Change your if() to skip over cases where i == j.

On second examination your code appears to work without this skip. What about your original code makes you think it doesn't work?
 
Carey Brown
Saloon Keeper
Posts: 3254
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Zack Virgo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also of note if I take out the ! in my last if statement I get a full line of all the numbers minus the second ones, but I need a list of all the unique numbers, so while it will return them all only once, I am still getting the non unique mixed in.

A better way to explain it, it outputs -1 1 20 -7 30 19 -100 66 -8 84 -24 -25 25 2 60 but 30, 19, -100, -8 and 20 are not unique in the array.
 
Carey Brown
Saloon Keeper
Posts: 3254
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zack Virgo wrote:Also of note if I take out the ! in my last if statement I get a full line of all the numbers minus the second ones, but I need a list of all the unique numbers, so while it will return them all only once, I am still getting the non unique mixed in.

A better way to explain it, it outputs -1 1 20 -7 30 19 -100 66 -8 84 -24 -25 25 2 60
but 30, 19, -100, -8 and 20 are not unique in the array.
Ah, this is where a clarification of "distinct" comes in handy.
Either
A) If you have  two or more values that are the same output only one of them.
or
B) If you have two or more values that are the same then don't add them to your output.

If you want (A) then your code works. If you want (B) then change your inner loop to go the full length of the array and put i != j in your if() statement.
 
Carey Brown
Saloon Keeper
Posts: 3254
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Either
A) If you have  two or more values that are the same output only one of them.
or
B) If you have two or more values that are the same then don't add them to your output.

As a point of comparison, "DISTINCT" is an SQL keyword and has the behavior of (A).
 
Zack Virgo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B is what I'm looking for, sorry for the confusion, so I should make my inner loop


that gives me the whole array output, and if I keep the j<myArray.length then I get no output. Sorry I know this is pretty basic, but I'm very new and trying to wrap my head around the concept.
 
Carey Brown
Saloon Keeper
Posts: 3254
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got the for() expression wrong. It should look just like your outer loop. Your if() needs both tests in it. You are looking for any element that is the same in both the inner and outer loops EXCEPT you want to skip over the case where both loops are referring to the same element, which would be true for ALL values.

 
Zack Virgo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes so much more sense! Thank you, I didn't even think of using the && and importing the ArrayList and List utilities makes more sense then doing so without it.

Thank you again, this definitely makes more sense now.
 
Carey Brown
Saloon Keeper
Posts: 3254
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.

And, if your requirements say you need to return an int[], then this helper method would be useful.

 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your query seems to be sorted but in addition:
welcome to the Ranch
 
Dave Tolls
Ranch Foreman
Posts: 3009
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Ah, this is where a clarification of "distinct" comes in handy.


It also highlights how naming can affect how people interpret code.

"distinct" only actually appears in the code.  In the OP (and other posts) Zack uses "unique" in the requirements.
 
Carey Brown
Saloon Keeper
Posts: 3254
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. I would suggest changing isDistinct to isUnique in the code for the sake of clarity.
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Pedantic mode]
Don't call List or ArrayList utilities, because you might confuse them with a utility class, which is different. This is an example of a utility class; you cannot put any data into it, but you can use it to manipulate Lists or other kinds of Collection.
 
Carey Brown
Saloon Keeper
Posts: 3254
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my stab at creating this functionality using streams. C&C welcome. Seems like there should be a more elegant way to do this.
 
Piet Souris
Rancher
Posts: 2019
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or perhaps a little old school:

 
Junilu Lacar
Sheriff
Posts: 11435
176
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have to remember that OP is still a beginner. I really doubt OP is going to be able to use streams or other kinds of data structures given that he hasn't even gotten past regular for-loops and arrays yet.

What are the learning goals of this exercise? Here are a few I can think of:
1. Learn how to use arrays
2. Learn how to iterate through arrays
3. Challenge the student to come up with algorithms for processing different arrays using for-loops
4. Learn how to manage the value of an array index

Let's analyze the algorithm using a small example and try to describe the thought process for arriving at a solution.

Let's say we were given {1, 0, -1, 3, 1, 0, 5}. Solving this in my head, I get {-1, 3, 5} as the result array. How did I get this result? Well, I scan through the array and note down which numbers I see only one time. This is the general description of the simplest algorithm I can use. Now I try to do a step-wise refinement so I can translate this to Java code.

Here's where you have to think outside the box a little bit. Just because you are given one array and asked to produce another one, that doesn't mean that your solution should only use two arrays. Also, you don't have to solve the main problem at one go. Complicated problems can often broken down into a series of sub-problems. The solutions to those sub-problems should be easier to solve individually. You can then use the solution to each sub-problem as a stepping stone towards solving the main problem.

Hint #1: you should create at least one more array to track the array elements that you have seen more than once in the array.
Hint #2: This intermediate array isn’t going to be an array of integer. Choosing a name like seenMoreThanOnce will give you an idea of the type of array it is.

I'll let you think this through a little bit, maybe it will give you some new ideas for how to code your solution.
 
Liutauras Vilda
Marshal
Posts: 4811
330
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP's instructions wrote:We were given an array and asked to create a new array containing only the unique elements from the given array.

Carey Brown wrote:You could first sort the array and then loop through it and if an entry is the same as the previous entry it is not unique.

I'd try to avoid such approaches on original data in almost all scenarios, unless it is specifically asked. Requirements doesn't say to modify a given array.

@OP
If you ever going to do that kind of stuff, I'd suggest first to copy an array and perfrom sorting on a copy. Otherwise you'd introduce side effects. That's mainly because if you pass in an array to a method let's say, and you modify this array's content, that means some other parts of the system can be affected. It is fine if such action is required and expected by the caller, though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!