DEV Community

John Samuel Obinna
John Samuel Obinna

Posted on

Data Structure and Algorithm Basics part[0]

Before you jump into Data Structure & Algorithm #mypersonalnotebook I would assume you have basic knowledge of the following:

  • Conditional
  • Repetition Construct (forloop & while loops)
  • Functions
  • Variable Scope
  • Recursion
  • Declaring Classes

Just basic stuff a JavaScript programmer will know.

Note:

  • All output will be displayed on the Console.
  • Data Structure & Algorithm will be broken into parts.
  • If you don't have a basic knowledge above-listed, I will recommend you grab a copy of Eloquent Javascript
  • All codes used here are available on my Github repository.

Array

Every programming language includes some form of array.
Arrays are the most common data structure in computer programming.

Array in JavaScript

Arrays in javascript are linear collection of elements where each element can be access via an index (numeric indexing).

How to Create Arrays in Javascript

The most recommended way of creating arrays in javascript is by Declaring a variable using the computed members access [] operator.

var myArray = [];

Enter fullscreen mode Exit fullscreen mode

When you create an array like this, you will have an array with a length value of 0. Let's play around to be sure.

var myArray =  [];
console.log(myArray.length)//0
Enter fullscreen mode Exit fullscreen mode

We can also create an array using the constructed pattern.

var myArray =  new Array();
console.log(myArray.length)//0
Enter fullscreen mode Exit fullscreen mode
Note:
  • Arrays in javascript are collections of anything(functions,objects,primitives e.t.c)
  • Counting begins at index 0 not 1

Accessing Array Elements

Array Elements can be accessed using the [] operator.

var names =  ["Somto", "Ezeh",];
console.log(names[0] + ' ' names[1])// Somto Ezeh
Enter fullscreen mode Exit fullscreen mode

Writing Array Elements

Assigning data to the array we use []

var numbers =  [];
for(var i= 0; i < 10; i++){
    numbers[i] = i+1;
}
console.log(numbers)//1,2,3,4,5,6,7,8,9,10
Enter fullscreen mode Exit fullscreen mode

Array from strings

Invoking the split() function on a string breaks up that string into separate parts and produces an array from that individual string.

var string = "I Love javascript ";
var makeArray = string.split(' ');
console.log(makeArray);//["I","Love","javascript"]
Enter fullscreen mode Exit fullscreen mode

Accesor Functions

With the javascript accessor function, we can access an element of an array and return some value.

indexOf()

The indexOf function loops through an array to see if the parameter passed to it, is found inside the target array and return the position.

 var names = ["John","Samuel","Samuel","Obinna"];
 var position = names.indexOf("Samuel");
 console.log(position);//1
Enter fullscreen mode Exit fullscreen mode

The indexOf() function returns the position of the first element that matches the argument or returns -1, if not found.

lastIndexOf()
The lastIndexOf function loops through an array to see if the parameter passed to it, is found inside the target array and returns the last position matched or returns -1, if not found.

 var names = ["John","Samuel","Samuel","Obinna"];
 var position = names.lastIndexOf("Samuel");
 console.log(position);//2
Enter fullscreen mode Exit fullscreen mode

toString() and join() returns a string representation of an array separated with commas.

  var myArray  = ["How to use","Accesor","Functions"];
  var makeString = myArray.join()
  console.log(makeString);//How to use,Accesor,Functions
Enter fullscreen mode Exit fullscreen mode

To get rid of the commas, you pass in an empty quote to the join() function.

  var myArray  = ["How to use","Accesor","Functions"];
  var makeString = myArray.join(" ")
  console.log(makeString);//How to use Accesor Functions
Enter fullscreen mode Exit fullscreen mode

The concat() and splice() function produces a new array from an existing array.
The concat() function combines more arrays and produces a new array, and the splice() function produces a new array from a subset of a target array.

concat()

var firstArray  = ["Princess", "Precious", "Patience", "Pessy"];
var secondArray = ["Adesunloye","Adedeji","Adetoro"];
var joinAll = firstArray.concat(secondArray);
console.log(joinAll);//["Princess", "Precious", "Patience", "Pessy","Adesunloye","Adedeji","Adetoro"]

Enter fullscreen mode Exit fullscreen mode

splice()

var names  = ["Princess", "Precious", "Patience", "Pessy"];
var newArray = names.splice(2,3);
console.log(newArray);//["Patience","Pessy"]
Enter fullscreen mode Exit fullscreen mode

Mutator Functions

pop()

The pop() function removes an element from the end of an Array and returns the value.

var numbers   = [1,2,3,4,5,6];
var remove  = numbers.pop();
console.log(remove);// 6
Enter fullscreen mode Exit fullscreen mode

Adding an element to an array
There are two ways to add an element to an array.
They are push() and unshift() but we are going to talk about the push() function first.

push()

The push() function adds an element to the end of an array.

var array = [1,2,3,4,5,6,7,8];
array.push(9)
console.log(array) // 1,2,3,4,5,6,7,8,9
Enter fullscreen mode Exit fullscreen mode

unshift()
The unshift() function adds an element to the beginning of an array.

var array = [2,3,4]; 
array.unshift(1);
console.log(array)//1234
Enter fullscreen mode Exit fullscreen mode

shift()
The shift() function removes an element from the beginning of an Array and returns the value

var array = ["I", "LOVE", "ARRAYS"];
var firstElementRemoved = array.shift(); 
console.log(firstElementRemoved);// I

//You can also decide to discard the return value 

var array = ["I", "LOVE", "ARRAYS"];
            array.shift();
console.log(array);//["LOVE", "ARRAYS"]
Enter fullscreen mode Exit fullscreen mode

splice()
The splice() function adds or removes an element from the middle of an array.
The following arguments are needed to add or remove an element(s) from the middle of an array.

  • The starting index (where you want to begin adding elements).
  • The number of elements to remove (0 when you are adding elements).
  • The elements you want to add to the array.
var array = [10,20,30,70,80,90];
var newArray = [40,50,60]; 
array.splice(3,0,newArray); 
console.log(array); //10,20,30,[40,50,60],70,80,90
Enter fullscreen mode Exit fullscreen mode

Elements passed into the splice() function can be a list and not necessarily a collection of arrays.

var array = [10,20,30,70,80,90];
array.splice(3,0,40,50,60);
console.log(array); //10,20,30,40,50,60,70,80,90
Enter fullscreen mode Exit fullscreen mode

Here is an example of using splice() to remove elements from an array:


var intro = ["Hello", "Callme", "John", "Samuel"];
var removeintro = intro.splice(1,2);
console.log(removeintro);//Callme,John 
Enter fullscreen mode Exit fullscreen mode

reverse()
The reverse() function reverses the order of the elements of an array.

var array = [1,2,3,4,5,6,7,8,9];
var reverseArray = array.reverse();
console.log(reverseArray);//9,8,7,6,5,4,3,2,1
Enter fullscreen mode Exit fullscreen mode

sort()
The sort() function sorts the elements of an array lexicographically: for more information about lexicographically

var names = ["Williams","Joe","John","Samuel","Somto","Ezeh"]; 
names.sort(); 
console.log(names);// Ezeh,Joe,John,Samuel,Somto,Williams
Enter fullscreen mode Exit fullscreen mode

Yay! All is working Fine, now let's see how the sort functions works with numbers.

var numbers = [1,2,3,4,50,60,70,80,5,6,7];
numbers.sort();
console.log(numbers);//1,2,3,4,5,50,6,60,7,70,80
Enter fullscreen mode Exit fullscreen mode

Oops...
Not what we expected. Don't worry we can fix this by passing a function to the sort() function.

function compare(num1, num2) {
   return num1 - num2;
 }
var nums = [1,2,3,4,50,60,70,80,5,6,7]; 
var sorted = nums.sort(compare);
console.log(sorted)// 1,2,3,4,5,6,7,50,60,70,80
Enter fullscreen mode Exit fullscreen mode

YES! It works.

Datastructure & Algorithm Basics part[1] will be available soon...

Top comments (0)