Q:

Erlang - Removing repeating tuples according to first value

I am trying to create a function that will look into a list of tuples and remove the ones that have identical first elements. For example: 
rmvSameTpl([{2,1}, {2,1}, {3,1}, {2,1}]).

should return [{2,1}, {3,1}].
The problem is my function always returns only the first tuple and as I'm a beginner I can't figure out why that is the case?
-export([rmvSameTpl/1]).

rmvSameTpl ([])-> [];
rmvSameTpl ([Z])-> [Z];
rmvSameTpl ( [H|T] ) -> 
    [H| [L || L<-rmvSameTpl(T), (element(1, H)) /= (element(1, T))] ].

Any help will be appreciated.

A:

Your approach has O(N^2) time complexity. There is obvious simple O(N*logN) solution:
1> lists:ukeysort(1, [{2,1}, {2,1}, {3,1}, {2,1}]).
[{2,1},{3,1}]

A:

You are doing a wrong comparison in the last case: (element(1, H)) /= (element(1, T)) instead of (element(1, H)) /= (element(1, L)). It fails with an exception, since T is a list and not a tuple. But in Erlang list comprehensions, failing filters are treated as false, so [L || L<-rmvSameTpl(T), (element(1, H)) /= (element(1, T))] ends up being the empty list.

