String str, word; int wordsLen, i, count, j, k; Map<Integer, String> map = new HashMap<>(); Scanner s = new Scanner(System.in); System.out.print("Enter the String: "); str = s.nextLine(); String words[] = str.split(" "); wordsLen = words.length; System.out.println("\n--occurences--"); for (i = 0; i < wordsLen; i++) { word = words[i]; count = 1; for (j = i + 1; j < wordsLen; j++) { if (word.equals(words[j])) { count++; for (k = j; k < (wordsLen - 1); k++) { words[k] = words[k + 1]; } wordsLen--; j--; } } map.put(count, word); System.out.println(word + " occurs " + count); count = 0; } System.out.println(map); }
Map<K, List<V>>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)