I'm trying to translate this algorithm I wrote in Java to Scala, but I'm having trouble with the containsValue()
method that is present in Java. I want to do something like if (hashMap.containsValue(value))
but I have looked through the scala documentation and have only found a contains(key) method. How do you implement or use hashmap.containsValue(value) in Scala I'm still new to Scala, but here's what I have so far in Scala:
def retString(s: String)
{
val map = new mutable.HashMap[Int, Char]
for (c <- s.toCharArray)
{
//if(!map.containsValue(c)) goes here
}
}
` The full algorithm I'm trying to convert is removeDuplicates I wrote in Java:
public static String removeDuplicates(char[] s)
{
HashMap<Integer, Character> hashMap = new HashMap<Integer, Character>();
int current = 0;
int last = 0;
for(; current < s.length; current++)
{
if (!(hashMap.containsValue(s[current])))
{
s[last++] = s[current];
hashMap.put(current, s[current]);
}
}
s[last] = '\0';
//iterate over the keys and find the values
String result = "";
for (Integer key: hashMap.keySet()) {
result += hashMap.get(key);
}
return result;
}