Question designing a function

andrews

Well-known member
Joined
Nov 22, 2011
Messages
167
Programming Experience
5-10
I have to make a special function where performance is a must
Given a sorted array of integers, I want to know :
- the number of integers who are unique
- the sum of those numbers
I have make such a function but, I guess, without performance because I pass the array twice.
I want a function who is passing the array one time.
The function could look :

private function sumunique(byref ar() as integer,byref number as integer) as integer
......
return sum
end function

Thanks for any response
 
Last edited:
OK,

I think I may have got what you are after. Try this code, is this what you are trying to achieve:-

VB.NET:
Public Class Form1
  Private Class UniqueNumberArray
    Public Property SumOfNumbers As Integer
    Public Property TotalNumbers As Integer
  End Class
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim NumberArray() As Integer = {2, 3, 3, 3, 4, 5, 5, 5, 6}
    MsgBox(SummeriseUniuqueIntegersFromArray(NumberArray).SumOfNumbers)
    MsgBox(SummeriseUniuqueIntegersFromArray(NumberArray).TotalNumbers)
  End Sub
 
  Private Function SummeriseUniuqueIntegersFromArray(ByVal NumberArray() As Integer) As UniqueNumberArray
    Dim NumberSummary As New UniqueNumberArray
    NumberSummary.SumOfNumbers = (From No In NumberArray Select No Group By No Into G = Group, TotalNumbers = Count()).Where(Function(x) x.TotalNumbers = 1).Sum(Function(x) x.No)
    NumberSummary.TotalNumbers = (From No In NumberArray Select No Group By No Into G = Group, TotalNumbers = Count()).Where(Function(x) x.TotalNumbers = 1).Count(Function(x) x.No)
    Return NumberSummary
  End Function
End Class
Cheers,

Ian.
 
Sorry, Ignore that last post since I called the routine twice by mistake. Try this instead:-

VB.NET:
Public Class Form1
  Private Class UniqueNumberArray
    Public Property SumOfNumbers As Integer
    Public Property TotalNumbers As Integer
  End Class
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim NumberArray() As Integer = {2, 3, 3, 3, 4, 5, 5, 5, 6}
    Dim MyNumberSummary As UniqueNumberArray = SummeriseUniuqueIntegersFromArray(NumberArray)
 
    MsgBox(MyNumberSummary.SumOfNumbers)
    MsgBox(MyNumberSummary.TotalNumbers)
  End Sub
 
  Private Function SummeriseUniuqueIntegersFromArray(ByVal NumberArray() As Integer) As UniqueNumberArray
    Dim NumberSummary As New UniqueNumberArray
    NumberSummary.SumOfNumbers = (From No In NumberArray Select No Group By No Into G = Group, TotalNumbers = Count()).Where(Function(x) x.TotalNumbers = 1).Sum(Function(x) x.No)
    NumberSummary.TotalNumbers = (From No In NumberArray Select No Group By No Into G = Group, TotalNumbers = Count()).Where(Function(x) x.TotalNumbers = 1).Count(Function(x) x.No)
    Return NumberSummary
  End Function
End Class

Is that what you are trying to get to?

Cheers,

Ian
 
IanRyder
I shall try it, maybe it gives correct results but I also know that solutions with SQL likes commands are not so performant.
Thanks
 
Hi,

Not really sure what you are saying here but the code works correctly and I have not used any LIKE commands from SQL that you refer to? The technique I have used here is called LINQ.

Does this solve your issue?

Ian
 
Ian,
Thanks for your response.
You are right, it are LINQ commands and I confess that I do not know them.
But I received from someone a solution also using LINQ commands and after testing I saw that they were not performant.
You could asked yourself why I like always a performant solution but in my program I must use the function, let us say, 100000000 times, that is the reason.
Regards,
 
I have tried again to make such a function and this is now the result.

Private Function SumUniqueIntegers(ByVal ar() As Integer, ByRef number As Integer) As Integer
Dim i, sum, mark As Integer
Dim up As Integer = ar.GetUpperBound(0)
For i = 0 To up-1
If ar(i) = ar(i + 1) Then
mark = 1
Else
If mark = 0 Then
sum = sum + ar(i)
number += 1
Else
mark = 0
End If
End If
Next
If mark = 0 AndAlso ar(up - 1) <> ar(up) Then
sum = sum + ar(up)
number += 1
End If
Return sum
End Function

I hope that this function is correct.
Can somebody check the correctness?
Many thanks.
Regards
 
Question resolved.
I designed a function that runs but 1 time through the array and gives the answers.
I thank all for helping me.
Regards
 
Back
Top