Find a single duplicate in a list of lists Netlogo

512 Views Asked by At

I am trying to find a duplicate between the sublists of a list , if I have this list

let listA [[-9 2] [-9 1] [-9 0][-9 -1][-9 -2][-9 -3][-9 -4][-8 0][-9 0]] 

and it is a restriction that this list can have only one sublist that can be repeated which in these case is [-9 0] I would like to save these two elements in 2 variables for example :

let element-x item 0 ? 
let element-y item 1 ?

but i don't actually know how to compare two sublist of a list with each other if they have the same elements.

After taking these variables(element-x element-y), i have to remove every sublist in listA that contain one of these variables -9 or 0 and save the remaining cordinates in a new list(list-cordinates)

I have already done this by taking this variables(of the duplicated sublist) as constants (for testing purposes) in these code below :

    globals [

  list-cordinates
  element-x
  element-y
]
 set element-x -9    
 set element-y  0

 foreach listA [

  if item 0 ? != element-x AND item 1 ? != element-y[

 let x item 0 ?
 let y item 1 ?

 set list-cordinates lput( list x y ) list-cordinates

 ]


]  

Now I just need these variables not be constants but the 2 items from the duplicated sublist of listA.

1

There are 1 best solutions below

0
On BEST ANSWER

This is quick and dirty, but find-dup should return the first duplicated item (in this case a sublist) in the list.

to go
  let listA [[-9 2] [-9 1] [-9 0][-9 -1][-9 -2][-9 -3][-9 -4][-8 0][-9 0]] 
  show find-dup listA
end

to-report find-dup [ c ]
  ;returns the first duplicated item, or false if no duplicates
  if length c = 1 [ report false ] ;we've run out of list before a dup is found
  ;compare the first element of the list to the rest
  let a first c          
  let b butfirst c
  ;does the first element match any remaining element?
  foreach b [
    if (a = ?) [report a ]  ;found a duplicate, report it.
  ]
  ;no matches. test the remainder of the list for a duplicate
  report find-dup but-first b  
end

The rest of your code should take it from there.