Vizury Interview Question
Software Engineer in TestsCountry: India
Interview Type: Phone Interview
Does it mean find first word in the file which is only occurrence in whole file?
2n complexity solution
Can use a map to mark occurrence for each word in the map. in next iteration find the word with count 1.
You can optimize it further to one iteration by maintaining additional metadata of words with 1 count
Use a LinkedHashSet (non java lovers, read up on it and implement a hash table mapping onto a doubly linked list ).
For opportunities in Waterloo/Toronto, Ontario area, ping me.
Use a LinkedHashSet (non java lovers, read up on it and implement a hash table mapping onto a doubly linked list ).
For opportunities in Waterloo/Toronto, Ontario area, ping me.
public static void main(String[] args) throws IOException {
BufferedReader reader=new BufferedReader(new FileReader(new File("C:\\Users\\shintog\\Desktop\\test.txt")));
String s="";
int temp=0;
List<String> list=new ArrayList<>();
String[] s2 = null;
Map<String, Integer> map=new LinkedHashMap<>();
while((s=reader.readLine())!=null){
s2=s.split(" ");
}
for (int i = 0; i < s2.length; i++) {
if(!map.containsKey(s2[i])){
map.put(s2[i], 1);
}else{
map.put(s2[i], map.get(s2[i]) +1);
}
}
for (Map.Entry<String , Integer> entry: map.entrySet()){
if(entry.getValue()==1){
list.add(entry.getKey());
}
}
System.out.println(list.get(0));
}
public static void main(String[] args) throws IOException {
BufferedReader reader=new BufferedReader(new FileReader(new File("C:\\Users\\shintog\\Desktop\\test.txt")));
String s="";
int temp=0;
List<String> list=new ArrayList<>();
String[] s2 = null;
Map<String, Integer> map=new LinkedHashMap<>();
while((s=reader.readLine())!=null){
s2=s.split(" ");
}
for (int i = 0; i < s2.length; i++) {
if(!map.containsKey(s2[i])){
map.put(s2[i], 1);
}else{
map.put(s2[i], map.get(s2[i]) +1);
}
}
for (Map.Entry<String , Integer> entry: map.entrySet()){
if(entry.getValue()==1){
list.add(entry.getKey());
}
}
System.out.println(list.get(0));
}
public static string FindUniqueString(string[] str)
{
Dictionary<string, int> mydict = new Dictionary<string, int>();
for (int i = 0; i < str.Length; i++)
{
if (!mydict.ContainsKey(str[i]))
{
mydict.Add(str[i],1);
}
else
{
mydict[str[i]]++;
}
}
foreach (KeyValuePair<string,int> kvp in mydict)
{
if (kvp.Value == 1)
{
return kvp.Key;
}
}
return "";
}
- ankitp2100 June 08, 2015