Click here to Skip to main content
14,325,968 members
Rate this:
Please Sign up or sign in to vote.
Okay am a beginner programmer and i DON'T have a computer science degree so bare with me. I run into a problem where i need to find an element in an array that appears only once. With my high school math i think this is effectively trying to search for the inverse of mode right? can anyone explain to me an algorithm that finds the opposite of mode in simple layman terms (please avoid the O(nLogn) jargon unless you can explain it) when given a data set?

I.e. given a dataset = [0, 0, 0, 0, 1, 1, 2, 6, 6, 6, 6, 8, 8 ] return the dataset = [2], wheres the math in this


Thank you in advance for your help and time
Posted
Updated 15-Apr-10 8:16am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

I'm not sure there is a formal algorithm for this (I could be wrong), but the simplest way is just to scan through: if the current element is not the same as either of the elements to either side, it is unique. Pretty simple, isn't it? Or am I missing something?

Oh, and the word you want is bear - I am not stripping at my desk, with or without you! :laugh:
   
Rate this:
Please Sign up or sign in to vote.

Solution 5

Use the inbuilt methods of the List(of ) class to find the min or max values, e.g.

Dim ints() As Integer = {11, 3, 5, 2, 8, 9}
Dim intList As New List(Of Integer)(ints)

MsgBox(intList.Min)
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Loop through the values, from element 1 to element 11.
Put a test in, if dataset[a] <> dataset [a-1] AND dataset [a] <> dataset[a+1] then ... {do what you need to do to flag it}.

In the dataset given, 2 would be flagged up, but it may be that there are several 'individual' items in a dataset, they could all be flagged.
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

The algorithm I use looks like this:
Dim Min As Integer = Data(0)
For Each val As Integer In Data
    If Min < val Then Min = val
Next

'Min now contains the first minimum value


Basically, set your minimum holder to the first element. Then iterate through the set: if the element is less than the current minimum, it becomes the minimum. If the element is the same as the current minimum, it is passed by. (This is useful only when dealing with complex objects where you are looking for the minimum of, say, MyUser.LastLogon.)

I leave it as an excercise to rewrite this to find the maximum value. ;P
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100