Filtering duplicates in arrays

BlitzMax Forums/BlitzMax Programming/Filtering duplicates in arrays

SystemError51(Posted 2012) [#1]
So quick and dirty question. I used the search function but could not find anything on the subject.

Say I have an array with a few floats. Is it somehow possible to quickly find out how often a number occurs in it?

Assume the array has this:

Local testArray:Float[5]

testArray[0] = 0.12
testArray[1] = 0.36
testArray[2] = 0.36
testArray[3] = 0.41
testArray[4] = 0.41


0.36 and 0.41 occur twice.

Is there a call to quickly find those duplicates?


col(Posted 2012) [#2]
Hiya,

I don't think a call already exists. Normally you would some kind of hash table to do that, but here is a quick and dirty way, not the fastest by any means!!!




SystemError51(Posted 2012) [#3]
Wow awesome =) You're a legend


col(Posted 2012) [#4]
Bear in mind that using Floats or Doubles can produce results that are very close to each but technically not the same, but near enough to be the same in real terms, so if you're going to use values that have 4 or more places after the decimal point then I'd think about using some kind of epsilon to help keep things in check or use a different method altogether. 2 places like you're suggesting above will be ok.


Kryzon(Posted 2012) [#5]
Is it somehow possible to quickly find out how often a number occurs in it?

A different take on it:
SuperStrict 
Framework BRL.Basic

Type arrayUtils
	'Returns the amount of occurrences of 'value' inside 'array'.
	Function findFloat:Int(array:Float[], value:Float)
		Local Occurrences:Int = 0
		
		For Local element:Float = EachIn array
			If element = value Then Occurrences:+1
		Next
		
		Return Occurrences
	End Function
End Type


Local testArray:Float[5]

testArray[0] = 0.12
testArray[1] = 0.36
testArray[2] = 0.36
testArray[3] = 0.41
testArray[4] = 0.41

Print arrayUtils.findFloat(testArray, 0.36)
Print arrayUtils.findFloat(testArray, 0.41)



SystemError51(Posted 2012) [#6]
I'm thanking you both for these little pieces of code :) They have helped me greatly!