ID:159939
 
I have a list full of numbers, and I want to know if a number is repeated within that same list.

How would I do that?
proc/repeated_elements(list/l)
. = list()

for(var/a = 1, a <= l.len, a ++)
if(l.Find(l[a]) < a)
//list.Find() locates the first instance of that
//element, so if the index returned is less than
//the current index, it's repeated.
. += l[a] //Add the repeated element to the list
//of repeated elements.
Another method:

proc/CountNumber(list/L, number)
. = 0
var/i = L.Find(number)
if(!i) return
do
++.
i = L.Find(number, i+1)
while(i)


Lummox JR
In response to Popisfizzy
Yours runtimes something shocking there Fizzy-boy.

#define DEBUG

proc/repeated_elements(list/l)
. = list()

for(var/a = 1, a <= l, a ++)
if(l.Find(l[a]) < a)
//list.Find() locates the first instance of that
//element, so if the index returned is less than
//the current index, it's repeated.
. += l[a] //Add the repeated element to the list
//of repeated elements.

mob
verb
test()
var/list/I = list("a","b", "a")
repeated_elements(I)


The provided runtime is:
runtime error: list index out of bounds
proc name: repeated elements (/proc/repeated_elements)
source file: Fixing this guys stuff up.dm,7
usr: Tiberath (/mob)
src: null
call stack:
repeated elements(/list (/list))
Tiberath (/mob): test()


Line 7 being: if(l.Find(l[a]) < a)
In response to Tiberath
The profanity is quite shocking Tibby-boy.
In response to Popisfizzy
Popisfizzy wrote:
proc/repeated_elements(list/l)
for(var/a = 1, a <= l, a ++)


Uhm, does that work?
I was under the impression that you could not compare a variable holding a floating point number to a variable holding the reference to a list?
Or in other words, shouldn't you use l.len here instead?

[Edit: Gah, two new replies while I was reading and replying to the posting]
In response to Tiberath
Yar, oversight I just noticed. It should be l.len, not l. I've fixed it.