I want to learn how to write for loop functions without declear "var" at the front of the code. For example, I want to remove duplicates in an interger Array. In C++/Java, I can do like that:
int removeDuplicate(vector<int> nums)
{
vector<int> output
Map<int,int> counter;
for(i = 0; i < nums.size(); i++)
{
if(!counter.has_key(nums[i]))
{
counter[nums[i]]=1; //add new key-value pair
output.push_back(nums[i]);
}
}
return output;
}
But in scala, how to use immutable variables to complete above task.
Do NOT use scala's internal functions, such as distinct. This question is about Scala implementation.
distinctis a method, not a function, so I guess that means you are allowed to use it? Note that in your C++ example,size(),has_key(),push_back(),operator[], etc. are also "internal functions", yet you seem to have no trouble using them, so what makes those "internal functions" different from other "internal functions"? And what is an "internal function" anyway?