Finding repeated items in a array using array manipulation methods

Hi all,
Hope you all are doing good in these critical days.

I am having a doubt on implementing a program which finds repeated items using array manipulation methods. I tried below program.


module tb;

  int c[$] = '{1,3,1,2,3,6};

  int fin[$];
  int y;
  initial
    begin
      fin = c.find(x) with(x == int'(c.find_last(y) with(y == x)));                                                                    
     //here how to remove item x in find_last array first and then find last element that matches x.
      $display("%p", fin);                                          
    end
  
endmodule

Thanks in advance.

Edit :-


fin = c.find(x) with((c.find_last_index(y) with(x == y))!=c.find_first_index(z)with(z ==x)));
fin= fin.unique();
//this is functional.
//also is it beneficial to use these methods in terms of time complexity rather than coding 
//an efficient function to find repeated items

In reply to sasi_8985:

This is more like a puzzle (code will need comments for others to understand) Based on your definition, I can’t be sure what classifies as “array manipulation methods” and what does not. Am i forced to use them ? Or can I ignore them if i chose to ?
I didn’t chase fixing your code.

Speaking for efficiency, the below 1-pass method looks more efficient. Of course, I am using a helper Associative Array here. (You can avoid the helper associate array, and chose to do a unique at the end, like in your code).


module tb;
 
  int c[$] = '{1,3,1,2,3,6};
 
  int fin[$];
  int y;
  int a[int];
  initial
    begin
      foreach(c[i]) begin
        if(a.exists(c[i])) begin
          if(a[c[i]]==1) begin fin.push_back(c[i]); a[c[i]]=2; end
          if(a[c[i]]==2) continue;
        end
        else a[c[i]]=1;
      end
      
      $display("%p",fin);
    end

endmodule

In reply to KillSteal:

Hi, thanks for the reply. I wanted to know if there are built-in functions then why not use them instead of writing user defined functions.

Thank you

In reply to sasi_8985:

If there was a built-in function to “finds repeated items in an array” that returns an array of repeated items, then, for sure go ahead and use that.

In this case, you are combining multiple built-in functions(find_first/last and unique), and not to mention, you have nested find.

While a single atomic built-in function may be the most efficient; a combination of built-in functions does not guarantee better performance or readable code.

As you noticed in your experiment, it’s not trivial to complete your code to get the desired output :)

Occam’s razor ?

In reply to sasi_8985:

You could be using all the possible array method locators or alternatively some queue support:


module tb;
 
  int c[$] = '{1,3,1,2,3,6};
  int res[$];
  initial
    begin
      c.sort(); // ordering
      // this will give you the unique string
//       $display("unique %p",c.unique());
      // OR
      foreach(c[jj]) begin
        res = c.find_index with ( item == c[jj] );
        if(res.size()>1) begin
          c.delete(res[0]); // delete to avoid reiterating
          $display("size: %0d -> c[%0d] -> %0d",res.size(),jj,c[jj]);
          $display("DUPLICATED");
          res  = {}; // empty
        end
      end
    end
endmodule

There could be many solutions, using and without using the array methods. Regards

In reply to Rsignori92:
Hi,
Thanks for your reply.
I just wanted to know if we can write Traditional programs(which are generally written as both of you wrote) only using built in methods of system verilog.

Thanks.