Finding unique/distinct values in a list

Hello all,

I’m wondering if there is a function in processing to find unique/distinct values in arrays/lists. I am trying to make a dropdown menu from an imported data set. For example, if I have an array of 5 elements that contains “bear”, “deer”, “moose”, “moose”, “deer”, I want the function to return “bear”, “deer”, and “moose”. I’m trying to avoid writing my own function.

Many thanks!

1 Like

It’s not really hard when you use hashmap

1 Like

Processing.org/reference/StringList_hasValue_.html
Processing.org/reference/StringList_append_.html
Processing.org/reference/StringList_array_.html

String[] animals = { "bear", "deer", "moose", "moose", "deer" };
StringList unique = new StringList();

println(animals);

for (final String a : animals)  if (!unique.hasValue(a))  unique.append(a);
animals = unique.array();

println(animals);

exit();
1 Like

Processing.GitHub.io/processing-javadocs/core/processing/data/StringList.html#getUnique--

String[] animals = { "bear", "deer", "moose", "moose", "deer" };
println(animals);

animals = new StringList(animals).getUnique();
println(animals);

exit();
3 Likes