Friday, July 8, 2022

Array Data Structure Using Java Script

 Array Data Structure Using Java Script : -


Array is a Data Structure which contain a fix number of items and these items should be of the same type. 

Basically there are Two things in Array,

1. Element - Each and Every data which is store in array called element.

2. Index - The location of element is called index. we identify the element with the help of index and index is always a numeric value.

Operations we performed on Array:- 
Traversing - visiting each and every element of array at least once is called traversing,  we can travers the array in using Java Script as well.

example - 

let traversingArray = [21,75,98,45,11,16]

for(var i = 0 ; i < traversingArray.length; i ++){
    console.log("Index",i +" value "+traversingArray[i])
}

output - 

Index 0 value 21
Index 1 value 75
Index 2 value 98
Index 3 value 45
Index 4 value 11
Index 5 value 16


Insertion - Enter a new element in array either in start or in end or we can enter the element on any position of array.

//Replacing the first index or any given index :- 
//Enter 25  in the starting of given array [0] index:- 

function insertElement(){
var insertionArray = [10,4,6,18,20]
for(var i = 0 ; i < insertionArray.length ; i++){
    if(i == 0){
        insertionArray[0] = 25
    }
}
console.log(insertionArray)
}

insertElement()

OutPut :- 

[ 25, 4, 6, 18, 20 ]

//Enter element in the starting of Array: -

//Enter 25  in the starting of given array [0] index:- 

function insertElement(){
    var pos = 0
var insertionArray = [10,4,6,18,20]
for(var i=insertionArray.length -1;i>= 0;i--){
   insertionArray[i+1] = insertionArray[i];
   insertionArray[i] = 25;
}
console.log(insertionArray);
}

insertElement();


output: -

[25,10,4,6,18,20]


//Enter 25 at the end of array:- 

function insertElement(){  
var insertionArray = [10,4,6,18,20]
insertionArray[insertionArray.length] = 25
 console.log(insertionArray);
 }

insertElement();

output: -

[10,4,6,18,20,25]

//Enter a new element in the middle or any where in array:- 

function insertionElement(){
   var pos = 5
   var x = 30
    var implementedArray = [12,34,54,44,76,56]
    for(var i = implementedArray.length -1; i >= pos -1; i-- ){
         implementedArray[i] = implementedArray[i - 1];
         implementedArray[2] = 30;
    }
    console.log(implementedArray)
}
insertionElement()

output:- 
[ 12, 34, 30, 44, 44, 76 ]


Deletion - Deleting a element from the array is called deletion, we can delete the item from the present array.

//Remvoe 32 From the array : -

function removeElement(){
    var removalArray = [23,43,65,32,76]
    for(var i = 0; i<= removalArray.length - 1; i ++){
       removalArray[i] = removalArray[i+1]
    }
    console.log("hii value",removalArray)
}
removeElement()

OutPut :- 
hii value [ 43, 65, 32, 76]

Search - Searching an element in array by value or by index is called searching.
// Search 52 in the given array: -

function searchElement(){
    var searchArray = [23,43,65,32,76,52]
    for(var i = 0; i<= searchArray.length - 1; i ++){
       if(searchArray[i] == 52){
           console.log(" we have found the value",searchArray[i])
       }
    }
}
searchElement()

OutPut : - 
we have found the value 52

Updated -  we can update the value of index in array.

//update the value 52 with 99
//Remvoe 52 in the array : -

function updateElement(){
    var updateArray = [23,43,65,32,76,52]
    for(var i = 0; i<= updateArray.length; i ++){
       if(updateArray[i] == 52){
          updateArray[i] = 99
       }
    }
    console.log(updateArray)
}
updateElement()

OutPut : -

[ 23, 43, 65, 32, 76, 99 ]

These are the operations we can performed on array using java script or any new programming language.
we see all these operations using Program in java Script.










No comments:

Post a Comment

Java Script

  1 Feb 2024 (1) Find the Second largest number in the array: - const findSecondLargest=(a)=>{     var largestNumber = 0;     var secondL...