1

how would I increment the key [i] by 1 in this situation every time I run through this for loop with the way I currently have it set up all the elements only get mapped to 1. I am trying to find out how many times each number occurs. I have tried +1 in the empty spot after list.get(i) but again only maps each element to 1. thank you.

    List<Integer> list = new ArrayList<Integer>();
    HashMap<Integer,Integer> Mode = new HashMap<Integer, Integer>();
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            list.add(arr[i][j]); 
        }
    }
    System.out.println(list);
    int count = 1;
    for(int i = 0; i < list.size(); i ++) {
        Mode.put(list.get(i), );
3
  • int count = 1 can be ignored!! Commented Jan 20, 2012 at 3:29
  • Your question is not very understandable, but comment is good Commented Jan 20, 2012 at 4:00
  • haha sorry been working on these assignments since 10AM MT time just out of it thanks for the help I really appreciate it Commented Jan 20, 2012 at 4:21

3 Answers 3

3

You need to specify a Key here.

for(int i = 0; i < list.size(); i++) {
        int value=list.get(i);
        if(!Mode.containsKey(value))
            Mode.put(value,1);
        else
            Mode.put(value,Mode.get(value)+1);
}
Sign up to request clarification or add additional context in comments.

1 Comment

@AVD is right on the money. Or your question is unclear. From what I understand, you are looking to count the number of occurances a value occurs within your array. Consequently, you use the value as the key to your map, and then the number of occurances as the map value. That is precisely what is being suggested. If this is incorrect, please clarify your question and explain why the solution offered is invalid.
2

According to your comment,

for(int i = 0; i < list.size(); i ++) {
   if(Mode.containsKey(list.get(i)) ){
       Integer count = Mode.get(list.get(i));

       Mode.put(list.get(i), ++count);}
   else
       Mode.put(list.get(i), 1);

Comments

1

If you have the option, you may find it easier to use something like Multiset from Guava.

Multiset<Integer> seen = HashMultiset.create();
for (int[] row : arr) {
  for (int elem : row) {
    seen.add(elem); // none of that nasty dealing with the Map
  }
}
// you can look up the count of an element with seen.count(elem)
E mostCommon = null;
int highestCount = 0;
for (Multiset.Entry<Integer> entry : seen.entrySet()) {
  if (entry.getCount() > highestCount) {
    mostCommon = entry.getElement();
    highestCount = entry.getCount();
  }
}
return mostCommon; // this is the most common element

1 Comment

If you can't use third-party libraries -- which may be the case, if this is a homework assignment -- you should use AVD's solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.