Skip to content

Instantly share code, notes, and snippets.

@KrashLeviathan
Created October 23, 2020 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrashLeviathan/ff6c86462e594ea06385e103fd6ca2cf to your computer and use it in GitHub Desktop.
Save KrashLeviathan/ff6c86462e594ea06385e103fd6ca2cf to your computer and use it in GitHub Desktop.
PL/SQL - Discarding duplicates when processing an array of data by maintaining a hashset (technically a map) of processed inputs
/*
This PL/SQL block demonstrates the use of an associative array and
the STANDARD_HASH function to discard duplicates when processing
an array of inputs from a PL/SQL array (VARRAY).
Written for & tested on an Oracle 12c database.
*/
DECLARE
TYPE t_hashmap IS
TABLE OF BOOLEAN INDEX BY VARCHAR2(40);
v_map t_hashmap;
v_hash VARCHAR2(40);
TYPE t_arr_varchar2 IS
VARRAY ( 5 ) OF VARCHAR2(100);
v_inputs t_arr_varchar2;
BEGIN
v_inputs := t_arr_varchar2('One','Two','One','Four','Two');
FOR i IN 1..v_inputs.count LOOP
SELECT standard_hash( v_inputs(i) )
INTO v_hash
FROM dual;
IF v_map.EXISTS(v_hash) THEN
CONTINUE;
END IF;
v_map(v_hash) := true;
dbms_output.put_line('Take Action on ' || v_inputs(i) );
END LOOP;
END;
/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment