Skip to content

Instantly share code, notes, and snippets.

@built
Created March 2, 2012 03:00
Show Gist options
  • Select an option

  • Save built/1955231 to your computer and use it in GitHub Desktop.

Select an option

Save built/1955231 to your computer and use it in GitHub Desktop.
Later PDXErlang intersection implementation w/ Tim. Much more performant.
-module(larray).
-include_lib("eunit/include/eunit.hrl").
-export([intersection/2]).
intersection(ListA, ListB) ->
Numbers = lists:foldl( fun(N, Ranking) -> dict:update_counter(N, 1, Ranking) end , dict:new(), ListA ++ ListB),
lists:usort([Unique || Unique <- dict:fetch_keys(Numbers), dict:fetch(Unique, Numbers) > 1]).
%% helper functions to create big lists of random integers
large_random_array(N, MaxValue) ->
large_random_array(N, MaxValue, []).
large_random_array(0, _, L) ->
L;
large_random_array(N, MaxValue, L) ->
large_random_array(N - 1, MaxValue, [random:uniform(MaxValue) | L]).
intersection_test_() ->
ListA = lists:usort(large_random_array(100000, 1000000)),
ListB = lists:usort(large_random_array(100000, 1000000)),
Result = lists:usort(sets:to_list(sets:intersection(sets:from_list(ListA), sets:from_list(ListB)))),
?_assertEqual(Result, intersection(ListA,ListB)).
@built
Copy link
Author

built commented Mar 2, 2012

More performant but much less Erlangy. Needs some pattern matching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment