A
A
Anton @ Lyalin2016-11-09 11:44:16
Java
Anton @ Lyalin, 2016-11-09 11:44:16

Why doesn't the HashMap option work?

It is necessary that each element of the array be counted, how many times it occurs.

static HashMap<Integer, String> number(String[] array){
        HashMap<Integer, String> map = new HashMap<>();
        int number = 0;
        for (int i = 0; i < array.length; i++){
            for (int j = 0; j < array.length; j++){
                if (array[i].equals(array[j])){
                    number++;
                    map.put(number, array[i]);
                }

            }
        }
        return map;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2016-11-09
@toxa_1995

1. Map is a structure like {Key, Value} - you have the key and value mixed up in places.
2. int number = 0 - must be in a different place.
3. Learn the methods of the Map class - this task can be solved in one loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question