Link to home
Start Free TrialLog in
Avatar of fsyed
fsyed

asked on

Need help with question on Arrays in Java

Dear fellow Java developers:

I'm having a problem involving arrays in Java that I need help with.  I a method:

public void removeEven(int [] numbers) {}

the numbers array looks like this:

[23,-32,5,23,43,-32,5,23,23]

I need to write a method so that I iterate through this array, identify the integer that appears an odd number of times (in this case 43), and then print it out.  One approach I was thinking of is to create another array, where each index of the second array represents each unique number in the first array.  As I iterate through the first array, every time I come across a duplicate number, I increment the index in the second array.  Then I would iterate through the second array, and identify the odd number(s) that are in the array.  My problem with this is that I'm not sure how really to implement it, nor do I think it is the best approach.  Can anyone figure out a solution to this?

Thanks in advance to all who reply.
Avatar of for_yan
for_yan
Flag of United States of America image


I suggest to make a  HashMap <Integer, Integer>
then you go through array and you populate this HashMap - if you enconter
the number first time you
put(encountered number, 0)
if number ios alreadty there
then
retrieve the number increment it and put again with the asame
key
then you iterate throu your hashmap
and find lll keys which correspond to odd values

Let me know if you have queastions
Avatar of fsyed
fsyed

ASKER

Thanks so much for your prompt reply, but unfortunately I've never worked with HashMaps.  Can you provide code to explain your solution?
Avatar of fsyed

ASKER

Are HashMaps the best way to solve this problem?
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually you can use vven less lines thamn here:

  if(m.get(numbers[j]) != null){
                int count = m.get(numbers[j]);
                m.put(numbers[j],count+1);
            }   else m.put(numbers[j],1);

Open in new window


but in this way it is more understandable

Read about HashMap in the API

http://download.oracle.com/javase/6/docs/api/java/util/HashMap.html




Avatar of fsyed

ASKER

Let me try your solutions, and no, I am not taking any java classes, this is to brush up my java skills because I have been programming in Objective-C for the past year :-)
Avatar of fsyed

ASKER

Thanks again for your prompt reply.  Can you explain what you are doing in the two "for" loops so that I get a better understanding of your solution?  Thanks again for your help.

So I'm setting up hashmap where my aray numbers are keys and values are number of their occurrences in the array
The eys in hashmap are unique, so when I put the new pair with thesame key then just the value for that
key is overwritten

In the first loop I'm going through array elements which I will use as keys to my HahsMap - so
each time I pick new element - and check if I already have sucvh key in the Hashhmap
  if(m.get(numbers[j]) != null){
if it exists - I retiruive teh value - how many time it already occurred - imncrement it and put to hahsmap
with the same key
if it does not exist i just increments zero and put value 1 - as ocurrence of this number is at this momemnt just 1
 after I exit frm this loop I have good hashmap where the keys will be unique subset of array mambers
and values - their ocuurrences in the array

secind for loopp is just iterations through all keys of the hashmap mm.keySet() is set of all keys
then for each key I get the value and I check if the vallue is odd i print the key

Let me know if something is not clear