Reputation: 6158
I have a String
which contains numbers in a comma separated form. and I want to extract the most appears numbers from the String.
For example I have a String like..
String str="1,2,3,4,5,6,7,19,18,4";
from the above str i need 4 because 4 is two times in str.
same as
String str2="1,2,3,4,6,4,3,9";
from the above str2 i need 3,4
In case of all numbers are unique then i need first one.
suggest me better approach.
Upvotes: 2
Views: 358
Reputation: 4233
Here's a solution using java.util and Guava collect.
Outline:
Word -> Count
Count -> Words
Code:
public class HighestCountFinder {
public static ImmutableSet<String> findWordsWithGreatestCount(String wordsString) {
List<String> words = split(wordsString, ",");
Map<String, Integer> wordsToCounts = toMapWithCounts(words);
Multimap<Integer, String> countsToWords = toInverseMultimap(wordsToCounts);
NavigableMap<Integer, Collection<String>> countsToWordsSorted = toNavigableMap(countsToWords);
Collection<String> wordsWithHighestCount = lastValue(countsToWordsSorted);
return ImmutableSet.copyOf(wordsWithHighestCount);
}
public static ImmutableList<String> split(String wordsString, String separator) {
Iterable<String> parts = Splitter.on(separator).split(wordsString);
return ImmutableList.copyOf(parts);
}
public static ImmutableMap<String, Integer> toMapWithCounts(Iterable<String> entries) {
Multiset<String> entriesWithCounts = HashMultiset.create(entries);
return toMap(entriesWithCounts);
}
public static <E> ImmutableMap<E, Integer> toMap(Multiset<E> multiset) {
ImmutableMap.Builder<E, Integer> immutableMapBuilder = ImmutableMap.builder();
for (Multiset.Entry<E> entry : multiset.entrySet()) {
immutableMapBuilder.put(entry.getElement(), entry.getCount());
}
return immutableMapBuilder.build();
}
public static <K, V> ImmutableMultimap<V, K> toInverseMultimap(Map<K, V> map) {
Multimap<K, V> multimap = Multimaps.forMap(map);
return ImmutableMultimap.copyOf(multimap).inverse();
}
public static <K, V> NavigableMap<K, Collection<V>> toNavigableMap(Multimap<K, V> multimap) {
return new TreeMap<>(multimap.asMap());
}
public static <V> V lastValue(NavigableMap<?, V> navigableMap) {
return navigableMap.lastEntry().getValue();
}
}
Upvotes: 0
Reputation: 41802
I would have an array of map of integers to count each number, and variable to store the number with the highest count.
Split the string into a list of numbers, and loop over the list.
Each time you look at a new number, increment its counter in the map, and if that count is greater than the current highest count, change the current highest to the current. At the end of the string, return the current highest.
Note that, if you also store the number with the second biggest count, you can optimise slightly by exiting the loop early, when there are not enough numbers left to allow the second highest count to exceed the highest.
Upvotes: 2
Reputation: 31627
Try Below Code
public class StringCheck {
public static void main(String args[]) {
String input = "1,2,3,4,5,6,2,3,4,1,4,33,33,33";
String result = "";
String[] arr1 = input.split(",");
System.out.println("Input is : " + input);
for (int i = 0; i < arr1.length; i++) {
int k = 0;
for (int j = 0; j < arr1.length; j++) {
if (arr1[i].equals(arr1[j])) {
k++;
if (k == 2) {
if (result.contains(arr1[i])) {
break;
}
result = result + arr1[i] + ",";
break;
}
}
}
}
System.out.println("Result is " + result);
}
}
Below will be output
Input is : 1,2,3,4,5,6,2,3,4,1,4,33,33,33
Result is 1,2,3,4,33,
Let me know if there are any queries...
Cheers!!!
Upvotes: 0
Reputation: 691635
Transform your String into a list of numbers.
Remember what the first number of the list is.
Sort the list of numbers.
Iterate over the list and keep
At each iteration, if the current number is equal to the current max, add the current number to the result. If it's greater than the current max, clear the set of results and add the current number to the results.
At the end of the loop, if the size of the result set is equal to the number of distinct numbers, return the first number you remembered before sorting the list.
Upvotes: 1
Reputation: 726479
Assuming that this is a homework, I'll suggest a general idea without the code:
','
charactersInteger
to Integer
with the countsint
values1
.1
, return the first token1
, go through the map again, and collect all keys where the value is equal to maxThis approach lets you work with very large numbers, and it does not discriminate between numbers with and without leading zeros. In other words, it will identify 3
in 1,2,03,3,3,2
sequence as a unique winner, not as a tie with 2
.
Upvotes: 2
Reputation: 13356
You can tokenize the string using java.util.StringTokenizer
using ,
as the separator, then strip off the leading and trailing spaces from each token using String.trim()
, then store them into an array of int
s using Integer.parseInt()
function. Then you can count them easily.
For counting:
If you have a small range of numbers, then you can simply create a counter array and use each number as an index to that array. If not, then you can use HashMap
or something like that.
Upvotes: 2