Microsoft Interview Question
InternsCountry: United States
public class DuplicateFrequency {
public static void main(String[] args) {
String str = "I am awesomely in Love With ZoomBA!";
char [] strArray = str.toCharArray();
int [] ar = {1,16,2,3,3,4,4,8,6,5,4};
Map<Character, Integer> map = new TreeMap<Character, Integer>();
for (int i=0; i<strArray.length ; i++) {
if (!map.containsKey(strArray[i]))
map.put(strArray[i], 1);
else
map.put(strArray[i],map.get(strArray[i]) +1) ;
}
map.forEach((k,v) -> System.out.println ("key ="+ k + ", No. of time Repeated "+ v) );
}
public class DuplicateFrequency {
public static void main(String[] args) {
String str = "I am awesomely in Love With ZoomBA!";
char [] strArray = str.toCharArray();
int [] ar = {1,16,2,3,3,4,4,8,6,5,4};
Map<Character, Integer> map = new TreeMap<Character, Integer>();
for (int i=0; i<strArray.length ; i++) {
if (!map.containsKey(strArray[i]))
map.put(strArray[i], 1);
else
map.put(strArray[i],map.get(strArray[i]) +1) ;
}
map.forEach((k,v) -> System.out.println ("key ="+ k + ", No. of time Repeated "+ v) );
}
func printUniqueCharacter(givenString: String) -> (value: Array<Any>, ley: Array<Any>) {
var myCharDict = Dictionary<Character,Int>()
for item in givenString.characters {
if myCharDict[item] != nil {
let value = myCharDict[item]
myCharDict.updateValue(value!+1, forKey: item)
} else {
myCharDict[item] = 1
}
}
let value = Array(myCharDict.values)
let key = Array(myCharDict.keys)
return (value,key)
}
Void stringStats(string& str) {
if(str.empty()) {
return;
}
int histo[(‘z’-’a’ + 1)] = {0};
for(size_t i(0); i < str.size(); ++i) {
histo[str[i]]++;
}
for(size_t i(0); i < (sizeof(histo)/sizeof(histo[0])); ++i ) {
if(histo[i] == 0) {
continue;
}
Std::cout << (‘a’+i) << “ : ” << histo[i] << std::endl();
}
}
var listGrouped = myString.GroupBy(x => x, //the element you want to group by
(key, //the element you grouped by
element //the new list of strings grouped
) => new
{
Key = key,
Count = element.Count()
});
foreach (var item in listGrouped)
{
Console.WriteLine("- - - - - - - -");
Console.WriteLine(item.Key);
Console.WriteLine(item.Count);
}
Console.ReadKey();
It gives this result:
- - - - - - - -
L
1
- - - - - - - -
e
1
- - - - - - - -
o
2
- - - - - - - -
n
1
- - - - - - - -
a
1
- - - - - - - -
r
1
- - - - - - - -
d
1
String text = "text to be tested";
char[] chars = text.toCharArray();
Map<Character,Integer> count = new HashMap<>();
for (char c : chars) {
if(count.containsKey(c))
{
int i = count.get(c);
count.put(c, ++i);
}
else
count.put(c, 1);
}
count.entrySet().stream().forEach(System.out::println );
/**
* Javascript Implementation.
* uniqueCharByCount
Given a string, print out all of the unique characters
and the number of times it appeared in the string
Input:
geeksforgeek
acaaabbbacdddd
Output:
{ g: 2, e: 4, k: 2, s: 1, f: 1, o: 1, r: 1 }
{ a: 5, c: 2, b: 3, d: 4 }
*/
function uniqueCharByCount(str) {
let charByCountMap = {};
for(let index = 0, length = str.length; index < length; index++) {
const char = str[index];
if( !charByCountMap[char] ) {
charByCountMap[char] = 1;
} else {
charByCountMap[char] = charByCountMap[char] + 1;
}
}
console.log(charByCountMap);
}
const str = 'acaaabbbacdddd';
console.log("uniqueCharByCount -> "+str+" \n");
uniqueCharByCount(str);
- Anonymous January 12, 2017