Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 185

# 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?

0
fsyed
• 4
• 4
1 Solution

Commented:

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)
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
0

Author Commented:
Thanks so much for your prompt reply, but unfortunately I've never worked with HashMaps.  Can you provide code to explain your solution?
0

Author Commented:
Are HashMaps the best way to solve this problem?
0

Commented:

This is the code ( idded 65 three tiimes to make it more interesting)

If you never used Hashmap - it is even better to use it.

There is only one more important class in Java household than HashMap
and that one is ArrayList - with these two you can program everything
(well I'm not talking of the String).

Let me know if you have any questions - I'll explain.
And I very much hope that you are stuidying java
for your own interest without any classes so that we would not be in violation
of EE rules

``````import java.util.HashMap;

public class CountOdds {
public static void main(String[] args) {

int[] numbers = {23,-32,5,23,43,-32,5,23,23,65, 65, 65};

HashMap<Integer,Integer> m = new HashMap<Integer,Integer>();
for(int j=0; j<numbers.length; j++){
if(m.get(numbers[j]) != null){
int count = m.get(numbers[j]);
m.put(numbers[j],count+1);
}   else m.put(numbers[j],1);

}

for(Integer in : m.keySet()){
int nn = m.get(in);
if((nn%2) ==1)System.out.println(in);

}

}

}
``````

``````65
43
``````
0

Commented:
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);
``````

but in this way it is more understandable

0

Author Commented:
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 :-)
0

Author Commented:
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.
0

Commented:

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

0

## Featured Post

• 4
• 4
Tackle projects and never again get stuck behind a technical roadblock.