user17983357
user17983357

Reputation:

Removing Duplicates from json_array_t

Currently i have two for loops that push ids into two separate json_array_t such as

[123,124,125,126]
[123,126,127]

I want to combine these two json_array_t into one and remove duplicates so it results in

[123,124,125,126,127]

to push ids from one json_array_t to another im doing this

ja_id1.put(1,id2,false);

But this puts all ids into one and keeps duplicates, and im not sure how to remove duplicates, thanks.

Upvotes: 0

Views: 360

Answers (1)

MT0
MT0

Reputation: 168026

Check for duplicates before adding the value:

DECLARE 
  arr1 JSON_ARRAY_T := JSON_ARRAY_T.PARSE('[123,124,125,126]');
  arr2 JSON_ARRAY_T := JSON_ARRAY_T.PARSE('[123,126,127]');
BEGIN
  <<outer_loop>>
  FOR i IN 0 .. arr2.get_Size - 1
  LOOP
    FOR j IN 0 .. arr1.get_size - 1
    LOOP
      IF arr1.get_number(j) = arr2.get_number(i) THEN
        CONTINUE outer_loop;
      END IF;
    END LOOP;
    arr1.append(arr2.get_number(i));
  END LOOP;

  FOR i IN 0 .. arr1.get_Size - 1
  LOOP
    DBMS_OUTPUT.PUT_LINE(arr1.get_number(i));
  END LOOP;
END;
/

Which outputs:

123
124
125
126
127

db<>fiddle here

Upvotes: 1

Related Questions