SlideShare uma empresa Scribd logo
1 de 17
PHP:

  Example #1 array_unique() example
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>

The above example will output:

Array
(
    [a] => green
    [0] => red
    [1] => blue
)

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
      Example #1 count() example
      <?php
       $a[0] = 1;
       $a[1] = 3;
       $a[2] = 5;
       $result = count($a);
       // $result == 3

       $b[0] = 7;
       $b[5] = 9;
       $b[10] = 11;
       $result = count($b);
       // $result == 3

       $result = count(null);
       // $result == 0

       $result = count(false);
       // $result == 1
       ?>
      Example #2 Recursive count() example
      <?php
       $food = array('fruits' => array('orange', 'banana', 'apple'),
                     'veggie' => array('carrot', 'collard', 'pea'));

       // recursive count
       echo count($food, COUNT_RECURSIVE); // output 8
// normal count
       echo count($food); // output 2

       ?>



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.     dim i, j, n as integer
        2.     dim list() as integer
        3.     n=0
        4.     for i = lbound(arr1) to ubound(arr1) - 1
        5.         for j = i + 1 to ubound(arr1)
        6.               if arr1(i) = arr1(j) then
        7.                   'here use "home made function" to delete the j-
       th item from the array
        8.                j=j-1
        9.              end if
        10.         next j
        11.    next i


home made function:

        1.     arr1(j) = arr1(ubound(arr1))
        2.     redim preserve arr1(lbound(arr1) to&nbs
       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   Use the VBScript Array method.
          o   Use the VBScript Split method.
          o   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.

For some of these tasks,   RhinoScript does provide useful methods.

      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


    Append another array to the end of an array
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


    Insert a new element at a given position in an array

 Sub ArrayInsert(ByRef arr, ByVal pos, ByVal val)
   Dim i
   If IsArray(arr) Then
     If pos > UBound(arr) Then
       Call ArrayAdd(arr, val)
     ElseIf pos >= 0 Then
       ReDim Preserve arr(UBound(arr) + 1)
       For i = UBound(arr) To pos + 1 Step -1
          arr(i) = arr(i - 1)
       Next
       arr(pos) = val
     End If
   End If
 End Sub


This example uses the .NET ArrayList object:

 Sub ArrayInsert(ByRef arr, ByVal pos, ByVal val)
   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.Insert(pos, val)
     arr = list.ToArray()
   End If
 End Sub


    Remove an element from the end of an array

 Sub ArrayRemove(ByRef arr)
   If IsArray(arr) Then
     If UBound(arr) > -1 Then
       ReDim Preserve arr(UBound(arr) - 1)
     End If
   End If
 End Sub


    Remove an element at a given position from an array

 Sub ArrayRemoveAt(ByRef arr, ByVal pos)
   Dim i
   If IsArray(arr) Then
     If pos >= 0 And pos <= UBound(arr) Then
       For i = pos To UBound(arr) - 1
          arr(i) = arr(i + 1)
       Next
       ReDim Preserve arr(UBound(arr) - 1)
     End If
   End If
 End Sub


This example uses the .NET ArrayList object:

 Sub ArrayRemoveAt(ByRef arr, ByVal pos)
   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.RemoveAt(pos)
     arr = list.ToArray()
   End If
 End Sub


    Remove all instances of a value from an array

 Sub ArrayRemoveVal(ByRef arr, ByVal val)
   Dim i, j
   If IsArray(arr) Then
     i = 0 : j = -1
     For i = 0 To UBound(arr)
If arr(i) <> val Then
          j = j + 1
          arr(j) = arr(i)
       End If
     Next
   ReDim Preserve arr(j)
   End If
 End Sub


    Remove duplicate items from an array

This example uses VBScript's Dictionary object:

 Sub ArrayCull(ByRef arr)
   Dim i, dict
   If IsArray(arr) Then
     Set dict = CreateObject("Scripting.Dictionary")
     For i = 0 To UBound(arr)
       If Not dict.Exists(arr(i)) Then
          Call dict.Add(arr(i), arr(i))
       End If
     Next
     arr = dict.Items
   End If
 End Sub


This example uses the .NET ArrayList object:

 Sub ArrayCull(ByRef arr)
   Dim i, list, tmp
   If IsArray(arr) Then
     Set list = CreateObject("System.Collections.ArrayList")
     For i = 0 To UBound(arr)
       If Not list.Contains(arr(i)) Then
          Call list.Add(arr(i))
       End If
     Next
     arr = list.ToArray()
   End If
 End Sub


    Find the first occurance of a value in an array

 Function ArraySearch(ByRef arr, ByVal val)
   Dim i
   ArraySearch = -1
   If IsArray(arr) Then
     For i = 0 To UBound(arr)
       If arr(i) = val Then
         ArraySearch = i
         Exit Function
       End If
Next
   End If
 End Function


    Sort the elements in an array

This example uses a simple Bubble Sort algorithm:

 Sub ArraySort(ByRef arr)
   Dim i, j, tmp
   If IsArray(arr) Then
     For i = UBound(arrShort) - 1 To 0 Step -1
       For j = 0 To i
          If arr(j) > arr(j + 1) Then
            tmp = arr(j + 1)
            arr(j + 1) = arr(j)
            arr(j) = tmp
          End If
       Next
     Next
   End If
 End Sub


This example uses the .NET ArrayList object:

 Sub ArraySort(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.Sort()
     arr = list.ToArray()
   End If
 End Sub


    Reverse the order of the elements in an array

 Sub ArrayReverse(ByRef arr)
   Dim i, ub, ub2, tmp
   ub = UBound(arr)
   ub2 = Int(ub / 2)
   For i = 0 To ub2
     tmp = arr(i)
     arr(i) = arr(ub - i)
     arr(ub - i) = tmp
   Next
 End Sub


This example uses the .NET ArrayList object:
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


   Verifies that an array has been "dimmed"

Function IsArrayDim(ByVal arr)
  IsArrayDim = False
  If IsArray(arr) Then
    On Error Resume Next
    Call UBound(arr)
    If Err.Number = 0 Then IsArrayDim = True
  End If
End Function


   Returns the number of dimensions to an array

Public Function GetArrayDim(ByVal arr)
  Dim i
  If IsArray(arr) Then
    For i = 1 To 60
       On Error Resume Next
       Call UBound(arr, i)
       If Err.Number <> 0 Then
         GetArrayDim = i - 1
         Exit Function
       End If
    Next
    GetArrayDim = i
  Else
    GetArrayDim = Null
  End If
End Function


   Print the contents of an array

 Sub ArrayPrint(ByVal arr)
   Dim i
   If IsArray(arr) Then
     For i = 0 To UBound(arr)
       Call Rhino.Print("Item(" &amp; CStr(i) &amp; ") = " &amp;
CStr(arr(i)))
     Next
   End If
 End Sub
Re: write vb script code to delete the duplicate values in an array.
Ans dim a                                                                     0 Guest
wer aList=Array(5,5,5,5,12,10,15,10,125,5)
# 1 Dim sNewList
      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.                           4     Jay
wer One simple algo which i followed was:                                         Praka
#2                                                                                   sh
      ''''''''''''''''''''''''''''''''''''''''''''''''
      ''''''''''''
      '''''''''''Script Starts
      Here'''''''''''''''''''''''''''''''

      Option Explicit

      Dim i, j, k, aMyArr, aNewArr( ), iNewIndex,
      iNewLim, bFlag

      bFlag = 0

      aMyArr = Array("AA","BB","AA", "BB" )
iNewIndex = UBound(aMyArr)

ReDim aNewArr(0)
aNewArr(0) = aMyArr(0)

For    i=0 To   UBound(aMyArr)

        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.
Is This Answer Correct ?   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
ReDim Preserve newArray(count)


      for j=0 to UBound(newArray)-1

      msgbox newArray(j)

      Next

       Is This Answer Correct ?   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)



If strHeading <> Location Then




DupCount(i,0) = Location
i=i+1
vLocCount = 1
DupCount(i,1) = 1

strHeading = Location
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
Array And Duplicate Values

I have an excel sheet containing values in column A and Column B. See bellow for
details.

ABC
_________________________________
aaaalbatros
bbbbirds
ccccanine
dddsomething
eeeeeeotherwise
dddasdblablas

i have a loop that goes through the first column and if the lengthe is > 4, the routines
picksup on ly the firt three letters of column B values and add a 1. However, because
all these values must be unique, i wrote a function that should check for duplicates.

I am trying to find duplicate values in the checkDup function. However, i am having
problems with it. I load the values in the array and the pass it to the function. if it is the
same then add 1 and check again until there are no duplicate. then that value is the
next unique value.

There is a twist: the first three letters can repeat and that is fine as long as the added
interger from the counter is different. But the counter should be reset to 1 when a new
set of values are present.

I need help with this. I am stuck.




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-with-
vbscript-using-the-mid-function/
http://thecareerplus.com/

Mais conteúdo relacionado

Destaque

Top 10 data engineer interview questions and answers
Top 10 data engineer interview questions and answersTop 10 data engineer interview questions and answers
Top 10 data engineer interview questions and answers
jomfari
 
Aws Wicked Rugby from yourprezi
Aws Wicked Rugby from yourpreziAws Wicked Rugby from yourprezi
Aws Wicked Rugby from yourprezi
Gordon Pong
 
Wicked rugby
Wicked rugbyWicked rugby
Wicked rugby
Dklumb4
 
Top 7 risk manager interview questions answers
Top 7 risk manager interview questions answersTop 7 risk manager interview questions answers
Top 7 risk manager interview questions answers
tomhandsome70
 

Destaque (10)

Top 12 skills for career success
Top 12 skills for career successTop 12 skills for career success
Top 12 skills for career success
 
Amazon Web Service Sales Role Play - Case Study
Amazon Web Service Sales Role Play - Case StudyAmazon Web Service Sales Role Play - Case Study
Amazon Web Service Sales Role Play - Case Study
 
Top 10 data engineer interview questions and answers
Top 10 data engineer interview questions and answersTop 10 data engineer interview questions and answers
Top 10 data engineer interview questions and answers
 
Cloud Computing Interview Questions
Cloud Computing Interview QuestionsCloud Computing Interview Questions
Cloud Computing Interview Questions
 
Google interview questions
Google interview questionsGoogle interview questions
Google interview questions
 
Aws Wicked Rugby from yourprezi
Aws Wicked Rugby from yourpreziAws Wicked Rugby from yourprezi
Aws Wicked Rugby from yourprezi
 
Top 10 math professor interview questions and answers
Top 10 math professor interview questions and answersTop 10 math professor interview questions and answers
Top 10 math professor interview questions and answers
 
Wicked rugby
Wicked rugbyWicked rugby
Wicked rugby
 
A Day in the Life of an Enterprise Architect (Role Play Exercise) 2016
A Day in the Life of an Enterprise Architect (Role Play Exercise) 2016A Day in the Life of an Enterprise Architect (Role Play Exercise) 2016
A Day in the Life of an Enterprise Architect (Role Play Exercise) 2016
 
Top 7 risk manager interview questions answers
Top 7 risk manager interview questions answersTop 7 risk manager interview questions answers
Top 7 risk manager interview questions answers
 

Amazon Interview Questions

  • 1. PHP: Example #1 array_unique() example <?php $input = array("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique($input); print_r($result); ?> The above example will output: Array ( [a] => green [0] => red [1] => blue ) 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  Example #1 count() example  <?php $a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count($a); // $result == 3 $b[0] = 7; $b[5] = 9; $b[10] = 11; $result = count($b); // $result == 3 $result = count(null); // $result == 0 $result = count(false); // $result == 1 ?>  Example #2 Recursive count() example  <?php $food = array('fruits' => array('orange', 'banana', 'apple'), 'veggie' => array('carrot', 'collard', 'pea')); // recursive count echo count($food, COUNT_RECURSIVE); // output 8
  • 2. // normal count echo count($food); // output 2 ?> 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. dim i, j, n as integer 2. dim list() as integer 3. n=0 4. for i = lbound(arr1) to ubound(arr1) - 1 5. for j = i + 1 to ubound(arr1) 6. if arr1(i) = arr1(j) then 7. 'here use "home made function" to delete the j- th item from the array 8. j=j-1 9. end if 10. next j 11. next i home made function: 1. arr1(j) = arr1(ubound(arr1)) 2. redim preserve arr1(lbound(arr1) to&nbs 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.
  • 3. o Use the VBScript Array method. o Use the VBScript Split method. o 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. For some of these tasks, RhinoScript does provide useful methods. 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 Append another array to the end of an array
  • 4. 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 Insert a new element at a given position in an array Sub ArrayInsert(ByRef arr, ByVal pos, ByVal val) Dim i If IsArray(arr) Then If pos > UBound(arr) Then Call ArrayAdd(arr, val) ElseIf pos >= 0 Then ReDim Preserve arr(UBound(arr) + 1) For i = UBound(arr) To pos + 1 Step -1 arr(i) = arr(i - 1) Next arr(pos) = val End If End If End Sub This example uses the .NET ArrayList object: Sub ArrayInsert(ByRef arr, ByVal pos, ByVal val) Dim i, list If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr)
  • 5. Call list.Add(arr(i)) Next Call list.Insert(pos, val) arr = list.ToArray() End If End Sub Remove an element from the end of an array Sub ArrayRemove(ByRef arr) If IsArray(arr) Then If UBound(arr) > -1 Then ReDim Preserve arr(UBound(arr) - 1) End If End If End Sub Remove an element at a given position from an array Sub ArrayRemoveAt(ByRef arr, ByVal pos) Dim i If IsArray(arr) Then If pos >= 0 And pos <= UBound(arr) Then For i = pos To UBound(arr) - 1 arr(i) = arr(i + 1) Next ReDim Preserve arr(UBound(arr) - 1) End If End If End Sub This example uses the .NET ArrayList object: Sub ArrayRemoveAt(ByRef arr, ByVal pos) 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.RemoveAt(pos) arr = list.ToArray() End If End Sub Remove all instances of a value from an array Sub ArrayRemoveVal(ByRef arr, ByVal val) Dim i, j If IsArray(arr) Then i = 0 : j = -1 For i = 0 To UBound(arr)
  • 6. If arr(i) <> val Then j = j + 1 arr(j) = arr(i) End If Next ReDim Preserve arr(j) End If End Sub Remove duplicate items from an array This example uses VBScript's Dictionary object: Sub ArrayCull(ByRef arr) Dim i, dict If IsArray(arr) Then Set dict = CreateObject("Scripting.Dictionary") For i = 0 To UBound(arr) If Not dict.Exists(arr(i)) Then Call dict.Add(arr(i), arr(i)) End If Next arr = dict.Items End If End Sub This example uses the .NET ArrayList object: Sub ArrayCull(ByRef arr) Dim i, list, tmp If IsArray(arr) Then Set list = CreateObject("System.Collections.ArrayList") For i = 0 To UBound(arr) If Not list.Contains(arr(i)) Then Call list.Add(arr(i)) End If Next arr = list.ToArray() End If End Sub Find the first occurance of a value in an array Function ArraySearch(ByRef arr, ByVal val) Dim i ArraySearch = -1 If IsArray(arr) Then For i = 0 To UBound(arr) If arr(i) = val Then ArraySearch = i Exit Function End If
  • 7. Next End If End Function Sort the elements in an array This example uses a simple Bubble Sort algorithm: Sub ArraySort(ByRef arr) Dim i, j, tmp If IsArray(arr) Then For i = UBound(arrShort) - 1 To 0 Step -1 For j = 0 To i If arr(j) > arr(j + 1) Then tmp = arr(j + 1) arr(j + 1) = arr(j) arr(j) = tmp End If Next Next End If End Sub This example uses the .NET ArrayList object: Sub ArraySort(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.Sort() arr = list.ToArray() End If End Sub Reverse the order of the elements in an array Sub ArrayReverse(ByRef arr) Dim i, ub, ub2, tmp ub = UBound(arr) ub2 = Int(ub / 2) For i = 0 To ub2 tmp = arr(i) arr(i) = arr(ub - i) arr(ub - i) = tmp Next End Sub This example uses the .NET ArrayList object:
  • 8. 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 Verifies that an array has been "dimmed" Function IsArrayDim(ByVal arr) IsArrayDim = False If IsArray(arr) Then On Error Resume Next Call UBound(arr) If Err.Number = 0 Then IsArrayDim = True End If End Function Returns the number of dimensions to an array Public Function GetArrayDim(ByVal arr) Dim i If IsArray(arr) Then For i = 1 To 60 On Error Resume Next Call UBound(arr, i) If Err.Number <> 0 Then GetArrayDim = i - 1 Exit Function End If Next GetArrayDim = i Else GetArrayDim = Null End If End Function Print the contents of an array Sub ArrayPrint(ByVal arr) Dim i If IsArray(arr) Then For i = 0 To UBound(arr) Call Rhino.Print("Item(" &amp; CStr(i) &amp; ") = " &amp; CStr(arr(i))) Next End If End Sub
  • 9. Re: write vb script code to delete the duplicate values in an array. Ans dim a 0 Guest wer aList=Array(5,5,5,5,12,10,15,10,125,5) # 1 Dim sNewList 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. 4 Jay wer One simple algo which i followed was: Praka #2 sh '''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''' '''''''''''Script Starts Here''''''''''''''''''''''''''''''' Option Explicit Dim i, j, k, aMyArr, aNewArr( ), iNewIndex, iNewLim, bFlag bFlag = 0 aMyArr = Array("AA","BB","AA", "BB" )
  • 10. iNewIndex = UBound(aMyArr) ReDim aNewArr(0) aNewArr(0) = aMyArr(0) For i=0 To UBound(aMyArr) 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.
  • 11. Is This Answer Correct ? 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
  • 12. ReDim Preserve newArray(count) for j=0 to UBound(newArray)-1 msgbox newArray(j) Next Is This Answer Correct ? 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) If strHeading <> Location Then DupCount(i,0) = Location i=i+1 vLocCount = 1 DupCount(i,1) = 1 strHeading = Location
  • 13. 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
  • 14. Array And Duplicate Values I have an excel sheet containing values in column A and Column B. See bellow for details. ABC _________________________________ aaaalbatros bbbbirds ccccanine dddsomething eeeeeeotherwise dddasdblablas i have a loop that goes through the first column and if the lengthe is > 4, the routines picksup on ly the firt three letters of column B values and add a 1. However, because all these values must be unique, i wrote a function that should check for duplicates. I am trying to find duplicate values in the checkDup function. However, i am having problems with it. I load the values in the array and the pass it to the function. if it is the same then add 1 and check again until there are no duplicate. then that value is the next unique value. There is a twist: the first three letters can repeat and that is fine as long as the added interger from the counter is different. But the counter should be reset to 1 when a new set of values are present. I need help with this. I am stuck. 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
  • 15. 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
  • 16. 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-with- vbscript-using-the-mid-function/