9

Introduction to Data Structures and Algorithms With Modern JavaScript.

 3 years ago
source link: https://dev.to/kashuhappy/introduction-to-data-structures-and-algorithms-with-modern-javascript-58li
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

1. Arrays.

An array is a single variable in JavaScript that keeps numerous elements,unlike other languages where array is a reference to several variables. When we wish to keep a list of elements and retrieve them with a single variable, we utilize it frequently.

In JavaScript, an array may hold different items such as Boolean, strings, and numbers, all of which can be stored in a single array.

1.1 Declaring an Array.

An array can be declared in one of the following two ways:

// Method 1:
let arr = [];

// Method 2:
let arr = newArray();

Enter fullscreen mode

Exit fullscreen mode

Method 1 is the most commonly used and preferred method above method 2 because when initializing;
Method 1:

// initialization and declaring
let arr = ["mango", "pineapple"];

Enter fullscreen mode

Exit fullscreen mode

Method 2:

// initialization and declaring
// array has 3 elements/strings
let arr = new Array ("Toyota", "Audi", "Porshe");

//array has 4 elements that are defined
let arr1 = new Array (1, 2, 3, 4);

//array has 4 undefined elements
let arr2 = new Array (4);

Enter fullscreen mode

Exit fullscreen mode

It is evident from the above example that arr1 has 4 items, however arr2 has 4 undefined elements instead of a single element 4. As a result, method 2 is not favored when working with integers, but it is good when working with Boolean and strings, as illustrated above.

In method 2 startup of part 3 can, however, be changed to:

//First create an array of 4 undefined elements
let fruits = new Array(4);

// Assign the array values
Cars[0] = "mango";
Cars[1] = "apple";
Cars[2] = "banana";
Cars[3] = "orange";

Enter fullscreen mode

Exit fullscreen mode

1.2 Accessing Items in an Array.

Because arrays are indexed from 0, a number in square brackets is used to access elements in an array.

let fruits = ["mango", "apple", "banana"];
console.log(fruits[0]); // mango
console.log(fruits[1]); // apple
console.log(fruits[2]); // banana

Enter fullscreen mode

Exit fullscreen mode

We already know that 0 always produces the first item in an array. You can use the length property, which we'll discuss later, to retrieve the final element in an array by performing the following procedure.

let fruits = ["mango", "apple", "banana"];
const lastItem = fruits.length -1;
console.log(fruits[lastItem]); // banana

//attempting to access a nonexistent element
console.log(fruits[5]); // returns undefined

Enter fullscreen mode

Exit fullscreen mode

You need add another index that corresponds to the inner array to be able to retrieve an item in a nested array.

let nestedArray = [
    [
        "mango",
        "banana",
    ],
    [
        "orange",
        "avocado",
    ]
];
console.log(nestedArray[1][1]); // avocado

Enter fullscreen mode

Exit fullscreen mode

1.3 Length property of an Array.

The number of elements in an array is returned using the length property of arrays.

An array's length attribute can be returned as:

let fruits = ["mango", "apple", "banana"];
console.log(fruits.length); // 3

Enter fullscreen mode

Exit fullscreen mode

However, to set the number of elements in an array, we may use the assignment operator with the length property.

let fruits = ["mango", "apple", "banana"];
fruits.length = 2;
console.log(fruits.length); // 2

Enter fullscreen mode

Exit fullscreen mode

1.4 Adding an Item to an Array.

We may assign a value to the next index to add a new value to our fruit variable, which has 3 items in the indices 0 to 2.

let fruits = ["mango", "apple", "banana"];
fruits[3] = "grape";
console.log(fruits);

Enter fullscreen mode

Exit fullscreen mode

Output:

[ 'mango', 'apple', 'banana', 'grape' ]

Enter fullscreen mode

Exit fullscreen mode

Push() can be used to add an item at the end of an array to avoid scenarios when you mistakenly skip an index while adding an item, resulting in an empty item or items in the array.

let fruits = ["mango", "apple", "banana"];
fruits.push("pineapple");
console.log(fruits);

Enter fullscreen mode

Exit fullscreen mode

Output:

[ 'mango', 'apple', 'banana', 'pineapple' ]

Enter fullscreen mode

Exit fullscreen mode

The unshift() function, on the other hand, may be used to add an item to the beginning of an array.

let fruits = ["mango", "apple", "banana"];
fruits.unshift("pineapple");
console.log(fruits);

Enter fullscreen mode

Exit fullscreen mode

Output:

[ 'pineapple', 'mango', 'apple', 'banana' ]

Enter fullscreen mode

Exit fullscreen mode

1.5 Removing an Item from an Array.

We utilize the splice() function to remove or delete a specific item from an array.

let fruits = ["mango", "apple", "banana"];
fruits.splice(1, 1);
console.log(fruits);

Enter fullscreen mode

Exit fullscreen mode

Output:

[ 'mango', 'banana' ]

Enter fullscreen mode

Exit fullscreen mode

There should be two parameters when using the splice() function. The first parameter specifies the index number to be eliminated (in our case, 1), while the second specifies the number of items to be removed. Otherwise, when one parameter is entered, the item in the index number enter is deleted, along with all subsequent items.

To delete the first item and the last item of an array, use the shift() and pop() methods, respectively. When feasible, however, it is preferable to use the pop() method since the rest of the items in the array will maintain their original index numbers.

//using pop() to remove last item
let fruits = ["mango", "apple", "banana", "pineapple"];
fruits.pop();
console.log(fruits);

//using shift() to remove first item from the remaining items
fruits.shift();
console.log(fruits);

Enter fullscreen mode

Exit fullscreen mode

Output:

[ 'mango', 'apple', 'banana' ]
[ 'apple', 'banana' ]

Enter fullscreen mode

Exit fullscreen mode

1.6 Looping Through an Array.

To loop through an array, we may use the for keyword to loop through the full array, taking use of the length parameter.

//create an array of vehicles
let vehicles = [
    "trucks",
    "vans",
    "buses",
    "lorries"
];

//loop through the length of the array
for (let i = 0; i < vehicles.length; i++) {
    console.log(i, vehicles[i]);
}

Enter fullscreen mode

Exit fullscreen mode

Output:

0 'trucks'
1 'vans'
2 'buses'
3 'lorries'

Enter fullscreen mode

Exit fullscreen mode

Although it does not obtain the index of each item, using for...of loop is a simpler and more succinct approach of looping through an array.

//create an array of vehicles
let vehicles = [
    "trucks",
    "vans",
    "buses",
    "lorries"
];

//loop through each vehicle
for (let vehicle of vehicles) {
    console.log(vehicle);
}

Enter fullscreen mode

Exit fullscreen mode

Output;

trucks
vans
buses
lorries

Enter fullscreen mode

Exit fullscreen mode


2. Queue

3. Linked list


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK