Hello friends, I hope you all are doing great. In this post today we will learn to solve the coding questions which is being asked by the Amdocs company recently. If you are preparing for selection in any company then this post will help you definitely.
Lets Start
Click here to get All Company Coding + Interview Questions and Programs
Return Map or HashMap
In this task you will get an array of string that contains words, you are required to return a map which will contain each unique word as a key, and the number of occurrences of each word as a value.\n\nFor example:
Given the following array of string:
"hello", "world", "this", "is", "this", "hello"
you will return:
{'hello': 2, 'world': 1, 'this': 2, 'is': 1}
Example :-
- Input:
- "hello", "world", "this", "is", "this", "hello"
- Output:
- {'hello': 2, 'world': 1, 'this': 2, 'is': 1}
Explanation:-
Frequency of hello is 2, world is 1 , this is 2 etc
Code in Python
# Code in Python class TestImpl: def hashMap(self, listString): mydict = {} for i in listString: if (mydict._contains_(i)): mydict[i] = mydict[i] + 1; else: mydict[i] = 1; print(mydict) obj = TestImpl() obj.hashMap(["hello", "world", "this", "is", "this", "hello"]) """ {'hello': 2, 'world': 1, 'this': 2, 'is': 1} """
Code in Java
import java.util.HashMap; import java.util.Scanner; class Range { static HashMap ReturnHashMap(String ar[]) { HashMap<String, Integer> map = new HashMap<>(); for (int i = 0; i < ar.length; i++) { if (!map.containsKey(ar[i])) { for (int j = 0; j < ar.length; j++) { if (ar[i] == ar[j]) { if (map.containsKey(ar[i])) { map.put(ar[i], map.get(ar[i]) + 1); } else { map.put(ar[i], 1); } } } } } return map; } public static void main(String[] args) { String arr[] = {"hello", "world", "this", "is", "this", "hello"}; HashMap<String, Integer> map = new HashMap<>(); map = ReturnHashMap(arr); System.out.println(map); } } /* OUTPUT {world=1, this=2, is=1, hello=2} */
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
0 Comments