Professional Documents
Culture Documents
See Also
count() - Count all elements in an array, or something in an object array_unique() - Removes duplicate values from an array array_values() - Return all the values of an array count_chars() - Return information about characters used in a string
Examples
hi to all iam using vb6.0 can any one tell me how to duplicate values in a array if possible i want to delete the duplicate values its urgent yes, there are many ways and methods, some faster than others. The worst, slower, but easier will be to check each one vs each one, lets say your array is Arr1
1. 2. 3. 4. 5. 6. 7. dim i, j, n as integer dim list() as integer n=0 for i = lbound(arr1) to ubound(arr1) - 1 for j = i + 1 to ubound(arr1) if arr1(i) = arr1(j) then 'here use "home made function" to delete the j-
th item from the array 8. j=j-1 9. 10. 11. end if next j next i
p;ubound(arr1) -1)
Overview
Arrays are a very useful and easy way of storing variables - and they're especially easy to use in VBScript. This is due to several factors:
VBScript is particularly liberal with any variable definition - that means that there is no strict defining of variables to a particular data type. The data type is assigned automatically when the variable is loaded with a value. It is even possible to mix data types within the same array. It is also possible to define the arrays in different ways: o Create the array element by element.
o o o
Use the VBScript Array method. Use the VBScript Split method. It is even possible to create multi-dimensional arrays and to make them dynamic rather than static.
But, VBScript falls short, compared to other programming languages, when it comes to providing tools for manipulating arrays. Some common array operations that VBScript does not provide methods for are:
Adding a new elements to an array. Appending one array to the end of another array. Inserting new elements at given positions in an array. Removing elements from an array. Removing duplicate items from an array. Sorting the elements in an array. Reverse the order of the elements in an array.
CullDuplicateNumbers - Remove duplicates from an array of numbers. CullDuplicatePoints - Remove duplicates from an array of 3-D points. CullDuplicateStrings - Remove duplicates from an array of strings. SortNumbers - Sorts an array of numbers. SortPoints - Sorts an array of 3-D points. SortStrings - Sorts an array of strings.
For the other tasks, you will need to write your own procedures. Fortunately, VBScript provides us enough tools to write our own procedures to do most of what we would ever want to do with an array. Note, the following examples are general purpose utilities. If you need something more specific, these examples are agood starting point.
Examples
Add a new element to the end of an array
Sub ArrayAdd(ByRef arr, ByVal val) Dim ub If IsArray(arr) Then On Error Resume Next ub = UBound(arr) If Err.Number <> 0 Then ub = -1 ReDim Preserve arr(ub + 1) arr(UBound(arr)) = val End If End Sub
Sub ArrayAppend(ByRef arr, ByVal arr0) Dim i, ub If IsArray(arr) And IsArray(arr0) Then On Error Resume Next ub = UBound(arr) If Err.Number <> 0 Then ub = -1 ReDim Preserve arr(ub + UBound(arr0) + 1) For i = 0 To UBound(arr0) arr(ub + 1 + i) = arr0(i) Next End If End Sub This example uses the .NET ArrayList object: Sub ArrayAppend(ByRef arr, ByVal arr0) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) Call list.Add(arr(i)) Next For i = 0 To UBound(arr0) Call list.Add(arr0(i)) Next arr = list.ToArray() End If End Sub
Call list.Add(arr(i)) Next Call list.Insert(pos, val) arr = list.ToArray() End If End Sub
If arr(i) <> val Then j = j + 1 arr(j) = arr(i) End If Next ReDim Preserve arr(j) End If End Sub
Sub ArrayReverse(ByRef arr) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) Call list.Add(arr(i)) Next Call list.Reverse() arr = list.ToArray() End If End Sub
Re: write vb script code to delete the duplicate values in an array. Ans dim a wer aList=Array(5,5,5,5,12,10,15,10,125,5) # 1 Dim sNewList 0 Guest
dim newArray b= ubound(aList) For x=0 to b If InStr(sNewList,(aList(x) & ",")) <= 0 Then sNewList = sNewList & aList(x) & "," End If Next newArray = split(sNewList,",") MsgBox sNewList
Is This Answer Correct ?
18 Yes
3 No
Re: write vb script code to delete the duplicate values in an array. Ans There are many ways this script can be written. wer One simple algo which i followed was: #2 4 Jay Praka sh
'''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''' '''''''''''Script Starts Here''''''''''''''''''''''''''''''' Option Explicit Dim i, j, k, aMyArr, aNewArr( ), iNewIndex, iNewLim, bFlag bFlag = 0 aMyArr = Array("AA","BB","AA", "BB" )
iNewLim = UBound(aNewArr) For j = 0 To iNewLim If aMyArr(i) = aNewArr(j) Then bFlag = 0 Exit For Else bFlag = 1 End If Next If bflag = 1 Then k=iNewLim+1 ReDim Preserve aNewArr(k) aNewArr(k)=aMyArr(i) End If Next ' To display the new array... For i = 0 to UBound(aNewArr) msgbox aNewArr(i) Next Erase aMyArr Erase aNewArr '''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''' '''''''''''Script Ends Here''''''''''''''''''''''''''''''' Please let me know if it satisfies ur query.
6 Yes
1 No
Re: write vb script code to delete the duplicate values in an array. Ans aList=Array(5,5,5,5,12,10,15,10,125,5,1,1,2,3,4, wer 5,6,7,8,8,8,8,8,8,8) #3
ReDim newArray(UBound(aList)) count=1 newArray(0)=aList(0) for i=0 to UBound(aList) temp=aList(i) flag=0 for j=0 to UBound(newArray) if ( newArray(j)=temp) Then flag=1 End if Next if flag=0 then newArray(count)=temp count=count+1 End If Next
4 Yes
1 No
Hello all. I have an array (SortedArray) which contains duplicates, and I would like to count each of them for a report. I know that I could use SQL to count them, but the results come from 20 different SQL queries. Anyways, below is the code that I have tried. I'm sure it is something simple, but I am so tired of looking at this code that I can't even think straight. I kind of have it, but I need for DupCount not to use 'v_AR_Tot' to size the array, and just use the size of the unique results. strHeading = "" strLoc = "" vLocCount = 0 Dim DupCount() ReDim Preserve DupCount (v_AR_Tot,6) i=0 For x=0 to UBound(SortedArray) Location = SortedArray(x,1)
Else vLocCount = vLocCount + 1 DupCount(i,1) = vLocCount end if Next For j=0 to UBound(DupCount) response.Write(DupCount(j,0) & " - " & DupCount(j,1) & "<BR>") next
Code: Option Explicit Dim MyArray(60) Dim MyArray2(30) Dim myval, myval2 As String Dim i, j, count As Integer Private Sub cmdAcct_Click() 'Dim i, j, count As Integer Dim myval, myval2 As String Range("C1:F60").Clear j=1
count = 0 For i = 2 To 60 Range("A" & i).Select If Len(Range("A" & i).Value) <= 4 Then Range("D" & i).Value = Range("A" & i).Value ElseIf Len(Range("A" & i).Value) > 4 Then count = count + 1 myval2 = Left(Range("b" & i).Value, 3) & count
'******************** this is to check for duplicate MyArray(j) = myval2 'assigns the value to the array myval = checkDup(MyArray(j)) 'then checks throught the function if it is a dup j=j+1 '******************** Range("D" & i).Value = myval
ElseIf Range("A" & i).Value = "" Then Range("D" & i).Value = "****" End If Next i 'Call checkDup Range("A1").Select End Sub Function checkDup(ParamArray MyArray()) Dim i As Integer Dim result As String '******************************** For i = 1 To UBound(MyArray) result = result & MyArray(i) & vbNewLine Next i MsgBox result End Function
var arr = [9, 9, 111, 2, 3, 4, 4, 5, 7]; var sorted_arr = arr.sort(); // You can define the comparing function here. // JS by default uses a crappy string compare. var results = []; for (var i = 0; i < arr.length - 1; i++) { if (sorted_arr[i + 1] == sorted_arr[i]) { results.push(sorted_arr[i]); } } alert(results);
Common questions:
Q: Given two numbers m and n, write a method to return the first number r that is divisible by both (e.g., the least common multiple). A: The Approach: What does it mean for r to be divisible by m and n? It means that all the primes in m must go into r, and all primes in n must be in r. What if m and n have primes in common? For example, if m is divisible by 3^5 and n is divisible by 3^7, what does this mean about r? It means r must be divisible by 3^7. The Rule: For each prime p such that p^a \ m (e.g., m is divisible by p^a) and p^b \ n, r must be divisible by p^max(a, b) The Algorithm: Define q to be 1. for each prime number p less than m and n: find the largest a and b such that p^a \ m and p^b \ n let q = q * p^max(a, b) return q Links: http://hpsqtp.blogspot.in/search?updated-min=2012-01-01T00:00:00%2B05:30&updated-
max=2013-01-01T00:00:00%2B05:30&max-results=2 http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/reversing-a-string-withvbscript-using-the-mid-function/
http://thecareerplus.com/