Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How in Scala to find unique items in List

Tags:

scala

How in Scala to find unique items in List?

like image 271
Volodymyr Bezuglyy Avatar asked Oct 08 '09 15:10

Volodymyr Bezuglyy


People also ask

How do you find unique elements in a Scala list?

Scala List distinct() method with example. The distinct() method is utilized to delete the duplicate elements from the stated list. Return Type: It returns a new list of elements without any duplicates.

How do I get distinct items from a list?

List<int> myList = list. Distinct(). ToList();

How do you check if an element is in a list Scala?

contains() function in Scala is used to check if a list contains the specific element sent as a parameter. list. contains() returns true if the list contains that element.

How does Scala distinct work?

Solution. The distinct method returns a new collection with the duplicate values removed. Remember to assign the result to a new variable. This is required for both immutable and mutable collections.


1 Answers

In 2.8, it's:

List(1,2,3,2,1).distinct  // => List(1, 2, 3) 
like image 125
jamesqiu Avatar answered Oct 06 '22 12:10

jamesqiu