473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Array Sort Algorithm to find duplicate values

i need to find a way to look through my _ReadingArrayList3 and see if any number stored in the inarrayindex are duplicated. I need to first collect all the data and place it into the array, once the array is filled i call my sort SortArray() Sub that will look at every arrayindex in the _ReadingArrayList3, at this time after that i can;t seem to find out how to compare values in the array. Below is the code i have been working on.

Thanks

Expand|Select|Wrap|Line Numbers
  1. Public Sub SortArraylist()
  2.  
  3.         For Each intarrayindex In _ReadingArrayList3
  4.             If _ReadingArrayList3.Then Then
  5.  
  6.             Else
  7.             End If
  8.         Next
  9.     End Sub
Sep 6 '07 #1
10 4105
Killer42
8,435 Expert 8TB
One tip - by using a Collection (or possibly a Dictionary) instead of an array, you can prevent duplicates being added.
Sep 6 '07 #2
Killer42
8,435 Expert 8TB
Here's a little routine that might be handy...

Expand|Select|Wrap|Line Numbers
  1. Public Function IsArrayEntryDuplicated(TheArray As Variant, WhichEntry As Long) As Boolean
  2.   Dim I As Long
  3.   For I = LBound(TheArray) to UBound(TheArray)
  4.     If TheArray(I) = TheArray(WhichEntry) Then
  5.       If I <> WhichEntry Then
  6.         IsArrayEntryDuplicated = True
  7.         Exit For
  8.       End If
  9.     End If
  10.   Next
  11. End Function
My syntax is probably not quite right, but hopefully you get the idea. Also, how well it works might depend on what version of VB you're using.
Sep 6 '07 #3
Here's a little routine that might be handy...

Expand|Select|Wrap|Line Numbers
  1. Public Function IsArrayEntryDuplicated(TheArray As Variant, WhichEntry As Long) As Boolean
  2.   Dim I As Long
  3.   For I = LBound(TheArray) to UBound(TheArray)
  4.     If TheArray(I) = TheArray(WhichEntry) Then
  5.       If I <> WhichEntry Then
  6.         IsArrayEntryDuplicated = True
  7.         Exit For
  8.       End If
  9.     End If
  10.   Next
  11. End Function
My syntax is probably not quite right, but hopefully you get the idea. Also, how well it works might depend on what version of VB you're using.

Killer42. Now im completely lost is there any way you can take the existing code in my message and work what your trying to do in there?

Thanks, im very new to .net
Sep 6 '07 #4
Killer42
8,435 Expert 8TB
You would place a reference to this function in line 4 of your code, I think.

The problem is, if you are doing For Each, then how do you know which entry you are looking at? The index, I mean. And if you don't know which one you're looking at, then how do you know whether any duplicate you find is actually the same entry?
Sep 6 '07 #5
hariharanmca
1,977 1GB
Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckArrayDuplicated(chkArray As Variant, Optional chkString As Variant, Optional lngChecked As Long)
  2.     If lngChecked <= 0 Then GoTo ReCallME
  3.     For i = LBound(MYARRAY) To UBound(chkArray)
  4.         If chkArray(i) = chkString And i <> lngChecked Then
  5.             MsgBox chkString & " - Duplicated in Array list"
  6.             GoTo ExitMe
  7.         End If
  8.     Next i
  9. ReCallME:
  10.     lngChecked = lngChecked + 1
  11.     If lngChecked < UBound(chkArray) Then chkString = chkArray(lngChecked) Else: GoTo ExitMe
  12.     CheckArrayDuplicated chkArray, chkString, lngChecked
  13. ExitMe:
  14. End Sub
you can call this method like
Expand|Select|Wrap|Line Numbers
  1. CheckArrayDuplicated MYARRAY
In this method, No need to sort.
Sep 6 '07 #6
kadghar
1,295 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. Private Sub CheckArrayDuplicated(chkArray As Variant, Optional chkString As Variant, Optional lngChecked As Long)
  2.     If lngChecked <= 0 Then GoTo ReCallME
  3.     For i = LBound(MYARRAY) To UBound(chkArray)
  4.         If chkArray(i) = chkString And i <> lngChecked Then
  5.             MsgBox chkString & " - Duplicated in Array list"
  6.             GoTo ExitMe
  7.         End If
  8.     Next i
  9. ReCallME:
  10.     lngChecked = lngChecked + 1
  11.     If lngChecked < UBound(chkArray) Then chkString = chkArray(lngChecked) Else: GoTo ExitMe
  12.     CheckArrayDuplicated chkArray, chkString, lngChecked
  13. ExitMe:
  14. End Sub
you can call this method like
Expand|Select|Wrap|Line Numbers
  1. CheckArrayDuplicated MYARRAY
In this method, No need to sort.
Hi Hari
woulnt the For be from lbound(chkarray) ¿?

i think it'll work even better if you run it from lngchecked +1 to ubound(chkarray)
... no need to check twice and you can even get rid of the condition i <> longchecked
Sep 6 '07 #7
hariharanmca
1,977 1GB
Hi Hari
woulnt the For be from lbound(chkarray) ¿?

i think it'll work even better if you run it from lngchecked +1 to ubound(chkarray)
... no need to check twice and you can even get rid of the condition i <> longchecked
I think that should be lngchecked -1 there. Because I already increase the value of lngchecked +1 in the first go to statement

Expand|Select|Wrap|Line Numbers
  1. If lngChecked <= 0 Then GoTo ReCallME
  2. .............
  3. ................
  4. ........
  5. ReCallME:
  6.     lngChecked = lngChecked + 1
  7. ...........
  8. ....
Iyes this function will check twice. It's just a concept! not an exact Algorithm

We can check once using another tmpArray.
Sep 7 '07 #8
Robbie
180 100+
I'm not sure if someone's already mentioned this way of doing it, and I did try looking at peoples' code, but I found it hard to understand because there weren't comments in the code.

I've done this before, although the way I did it gets exponentially slower as it finds more values which aren't duplicates; in other words it's much faster between 0->50% done than 50%->100%.


You start with an array which you want to check for errors.
You make a new array. This will hold every value in the original array which has been seen so far which is not a duplicate.

You loop through every value in the original array. At each loop, we are testing to see if this index contains a duplicate value of any previous indeces we've looked at.

To do this, it compares the value in the original array at the current index, with every single value in the new array. If it matches, then there is a duplicate.

When it's done, and if there was never any moatch (so it's NOT a duplicate), we add the value in original array of the current index, to this new array. That way, the next time round, THIS line which we just checked will be included in the test for duplicates, if you see what I mean.
(This is why it gets slower - it has to keep checking against more and more values in the new array)
Sep 7 '07 #9
hariharanmca
1,977 1GB
I'm not sure if someone's already mentioned this way of doing it, and I did try looking at peoples' code, but I found it hard to understand because there weren't comments in the code.
.......................
....................
...............
(This is why it gets slower - it has to keep checking against more and more values in the new array)
Let me explain some thing.
Let us welcome all the reply first then; we will research for the best and faster. We can get the best only by comparing solutions.
Just wait and see.
Sep 7 '07 #10
As someone said up the line, use a collection. As in:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim myAry() As Integer = {3, 6, 4, 7, 5, 5, 3, 8, 9, 1, 3}
  3.         Dim myCollection As New Collection
  4.         For x As Integer = 0 To UBound(myAry)
  5.             Try
  6.                 myCollection.Add(myAry(x), myAry(x))
  7.             Catch
  8.             End Try
  9.         Next
  10.         ReDim myAry(myCollection.Count)
  11.         For x As Integer = 1 To myCollection.Count
  12.             myAry(x) = myCollection.Item(x)
  13.         Next
  14.     End Sub
  15.  
Sep 7 '07 #11

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

Similar topics

18
by: Dan | last post by:
hello, I would to know if it is possible to delete an instance in an array, The following does not allow me to do a delete. I am trying to find and delete the duplicate in an array, thanks ...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
5
by: Paulers | last post by:
Hello all, I have a string array with duplicate elements. I need to create a new string array containing only the unique elements. Is there an easy way to do this? I have tried looping through...
11
by: cdg | last post by:
Could anyone tell me how to write a loop for an array, that would have to check each iteration for duplications of previous entered values. So, the exact number of loops is not known, but the total...
9
by: Jae | last post by:
Hi I wonder how can I implement the STL map sorting by value. For example, I have a map m map<int, intm; m = 10; m = 5; m = 6;
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
11
by: holla | last post by:
Write the following functions that can be called by a C program: Write a function to populate an array with random integers between 0 and 99. Use the following functions of which the prototypes...
6
by: Peter | last post by:
Hi I have a number of arrays of longs, from which I need to find a single array which only contains the values which appear in all the original arrays. For example, I could have the three...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.