By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,333 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,333 IT Pros & Developers. It's quick & easy.

Select distinct fields in an array

100+
P: 375
Hello
I am preparing an application where I have different data stored in an array
eg. Dim Vehicle(100) as String
In this array different values are stored in an ordered manner.

eg. Vehicle(0)="Tata"
Vehicle(1)="Tata"
Vehicle(2)="Tata"
Vehicle(3)="Maruti"
Vehicle(4)="Maruti"


and so on

I want only distinct values of the array to be stored in another array
eg. Vehicle(100)
Vehicle(0)="Tata"
vehicle(1)="maurti"

How can I select distinct fields from an array?
These are not stored in database, hence I cannot used the "select distinct" query.

Kindly help
Thanks
Jul 26 '07 #1
Share this Question
Share on Google+
8 Replies


Expert 5K+
P: 8,434
I believe the most direct method is simply to check whether each one exists in the second array before adding it. For example...
Expand|Select|Wrap|Line Numbers
  1. Dim Array2Count As Long, Used As Boolean
  2. Dim I As Long, J As Long
  3. For I = 1 To 100
  4.   Used = False
  5.   For J = 1 To Array2Count
  6.     If Array2(J) = Array1(I) Then
  7.       Used = True
  8.       Exit For
  9.     End If
  10.   Next
  11.   If Not Used Then
  12.     Array2Count = Array2Count + 1
  13.     Array2(Array2Count) = Array1(I)
  14.   End If
  15. Next
Of course I haven't tested this, just typed it here as I made it up. You may need to play with it a bit. Note, while VB prefers to start arrays at zero, I always start my arrays at one. You may need to adjust the code to suit your way of working.



Another option would be to add each entry to a collection, using the vehicle as the key. Duplicates will be rejected, and you can use On Error processing to skip over them. This is probably more bother than it's worth, though.
Jul 26 '07 #2

100+
P: 375
I believe the most direct method is simply to check whether each one exists in the second array before adding it. For example...
Expand|Select|Wrap|Line Numbers
  1. Dim Array2Count As Long, Used As Boolean
  2. Dim I As Long, J As Long
  3. For I = 1 To 100
  4.   Used = False
  5.   For J = 1 To Array2Count
  6.     If Array2(J) = Array1(I) Then
  7.       Used = True
  8.       Exit For
  9.     End If
  10.   Next
  11.   If Not Used Then
  12.     Array2Count = Array2Count + 1
  13.     Array2(Array2Count) = Array1(I)
  14.   End If
  15. Next
Of course I haven't tested this, just typed it here as I made it up. You may need to play with it a bit. Note, while VB prefers to start arrays at zero, I always start my arrays at one. You may need to adjust the code to suit your way of working.



Another option would be to add each entry to a collection, using the vehicle as the key. Duplicates will be rejected, and you can use On Error processing to skip over them. This is probably more bother than it's worth, though.

Thanks Killer

Actually what I did was I created a temporary variable and checked all the elements in the loop .(Beacuse the elements in the loop were in ordered manner my problem got solved much quicker)
If the element existed then move to next item in the loop
else add the element in the variable
eg. vehicleid=vehicleid+","+actualvehicleid

So all the unique vehicle names were added to the variable
and to separte all the vehicles I used the delimiter "," to seperate it

Thanks indeed for your idea too
Jul 28 '07 #3

Expert 5K+
P: 8,434
That's a clever idea. Wish I'd thought of it first. :)

Of course, as you point out, the fact that it's a single variable does allow you to check it quicker and more simply. I expect you just used Instr() function for the check?
Jul 29 '07 #4

100+
P: 375
That's a clever idea. Wish I'd thought of it first. :)

Of course, as you point out, he fact that it's a single variable does allow you to check it quicker and more simply. I expect you just used Instr() function for the check?
Killer 42, Actually I was supposed to write the program in C#(it was changed by my project leader).
And in C# you have a function called as split.
I used that.
Jul 29 '07 #5

Expert 5K+
P: 8,434
Killer 42, Actually I was supposed to write the program in C# (it was changed by my project leader).
And in C# you have a function called as split.
I used that.
VB also has a Split() function, but I don't see the relevance.
Jul 30 '07 #6

100+
P: 375
VB also has a Split() function, but I don't see the relevance.
the whole project was to be rewritten in C# console application
all the vehicle id's after sorting and checking from the loop are stored in a variable called as vehicleid(which i have mentioned earlier)
Now this will be stored in the manner tata,hyundai,marut etc.
Now I will the below function
and can manipulate whenever and whatever way i need it

Expand|Select|Wrap|Line Numbers
  1. string MainString = finalvehicleid;
  2.            string[] Split = MainString.Split(new Char[] { ' ,' });
  3.            int s1 = Split.Length;
  4.            string final;
  5.  
  6.             for (int i = 0; i < s1; i++)
  7.  
  8.             {
  9.                 final = Split[i].ToString();
  10.                 if (final != ",")
  11. // in case there are words which may (rarely) follow two quotes then i will not include it.
  12. //i will store these vehicle id and send mail to the required person using smtp
  13. //At present i have used message box but i will be required to display all the vehicles in a textbox
  14. //Because the vehicle id were not stored in a database i could use the select distinct query
  15.  
  16.                 {
  17.                     MessageBox.Show(final);
  18.                 }
  19.  
  20.  
  21.             }
Jul 30 '07 #7

Expert 5K+
P: 8,434
Correct me if I'm wrong, but doesn't that just split the string into an array, at the commas? You can certainly do the same thing with the Split() function in VB, but how is that any better than using an array in the first place, or adding to both at the same time?

I was enquiring as to how you prevented adding duplicates, and I can't see how that routine would do so. Keeping in mind that I don't know C#, of course.
Jul 30 '07 #8

100+
P: 375
Correct me if I'm wrong, but doesn't that just split the string into an array, at the commas? You can certainly do the same thing with the Split() function in VB, but how is that any better than using an array in the first place, or adding to both at the same time?

I was enquiring as to how you prevented adding duplicates, and I can't see how that routine would do so. Keeping in mind that I don't know C#, of course.

because it is a live project i will not be able to reveal much, also i will produce the code in vb.net but without using split.

Now we have a program where i have sorted some datas and grouped it by registration no and vehicle no
Expand|Select|Wrap|Line Numbers
  1.  
  2.  RowCount = (Ds.Tables("ChkTable").Rows.Count)
  3.                 UserId = Ds.Tables("chktable").Rows(0)(2)
  4.                 RegNo = Ds.Tables("chktable").Rows(0)(0)
  5.                 TempUserId = UserId
  6.                 TempStoreRegno = ""
  7. RowCount = (Ds.Tables("ChkTable").Rows.Count)
  8.                 For I = 0 To RowCount - 1
  9.                     RegNo = (Ds.Tables("chktable").Rows(I)(0))
  10.                     dataTime = (Ds.Tables("chktable").Rows(I)(1))
  11.                     UserId = (Ds.Tables("chktable").Rows(I)(2))
  12. 'Some calculations take place
  13.  'Send email to the User stating that his vehicle bearing this registration No is idle
  14.                     If (TempUserId = Ds.Tables("chktable").Rows(I)(2)) Then
  15.                         TempStoreRegno = TempStoreRegno + " " + Ds.Tables("chktable").Rows(I)(0)
  16.                     Else
  17.                         Call Display()
  18. 'call a function
  19.                         TempUserId = Ds.Tables("chktable").Rows(I)(2)
  20.                         TempStoreRegno = Ds.Tables("chktable").Rows(I)(0)
  21.                     End If
  22.                 Next
  23.                 Call Display()
  24. Call MessageMail
  25. 'This function will be used for some purpose
' if here the function is NOT called the last group will not be recieved for further process. because when it encounter the NEXT statement in loop and there is none to be found it will exit the loop hence to record the last group we call the function after next for the last group

Expand|Select|Wrap|Line Numbers
  1. Private Sub Display()
  2.         TempUserId1 = (Ds.Tables("chktable").Rows(I - 1)(2))
  3.         SuperUserId = SuperUserId + " " + vbCrLf + TempUserId1 + "  " + TempStoreRegno
  4.         Registrationno = Registrationno + vbCrLf + TempStoreRegno
  5.     End Sub
Hope I have cleared your doubts.
Thanks
Jul 30 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.