Click here to Skip to main content
15,883,769 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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:
 
Share this answer
 
Use the inbuilt methods of the List(of ) class to find the min or max values, e.g.

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

MsgBox(intList.Min)
 
Share this answer
 
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.
 
Share this answer
 
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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900