1

I have a set of elements {1,2,3},{2,3,4},{1,2,4},{7,8},{3,4,7,9},{12,16,18,19}, {1,2,4}.

I need to have a data structure which contains the list above appeared only once. If any new list appears and if it matches any of the existing list then I don't want that to be added to the resulting data structure.

For the above example the expected result should be : {1,2,3},{2,3,4},{1,2,4},{7,8},{3,4,7,9},{12,16,18,19}.

One solution which I am having is to use Trees. For ex: {1,2,3},{1,2,4} In the above list I will branch out for value 3 of 1st list and value 4 of 2nd list from the node which has value 2. In this way I can trace the list from the root and find whether the list appears or not.

     Root
      |
      |
      1
      |
      |
------2------
|            |
|            |
3            4

Please suggest if is there any algorithm to make it faster and in a simple way using C.

  • Classic Graph! Does the order matter ? Directed graph – Himanshu Tyagi Aug 09 '16 at 12:27
  • In this way you have to create a tree, for every list beginning with new number...Check http://stackoverflow.com/questions/268672/is-there-a-no-duplicate-list-implementation-out-there – Rupsingh Aug 10 '16 at 06:19

1 Answers1

0

You can achieve this using graph data structure.

To do so in c, you can use either of the following methods :

  1. Adjacency Matrix

In this method, you need to maintain a matrix of relationship between your numbers, check out the below image :

Adjacency Matrix

  1. Linked list :

In this method, you will maintain a list of connections, i.e if 2 has a connection to 1,3,4 then a list of 1,3,4 will be made with a head value of : 2

enter image description here

These images are taken from : http://simplestcodings.blogspot.in . And you can find great explaination and example code on the same site at : http://simplestcodings.blogspot.in/2013/09/graphs.html link

Hope it helps.

Himanshu Tyagi
  • 5,201
  • 1
  • 23
  • 43