## DEV Community is a community of 787,688 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Raksha Kannusami

Posted on

# Java: Arrays!

Most interviews for an SDE (software development engineer) role require you to have a good grasp of Data structures and algorithms.

One important data structure is Arrays.

An array is a linear data structure and is famous for storing values at contiguous memory locations.

I will be using Java, but don't worry, you should be able to understand these pseudo-codes irrespective of programming language.

Let us understand the basic manipulation on arrays like insert, delete, sort, etc.

# 1. Create an array.

To create an integer array of size 100,

``````int[] array = new int[100];
``````

# 2. Insert an element into an array.

The following function will insert any element at any position specified by the user and will return the size of the modified array after insertion.

``````int insert(int array[], int n, int x, int capacity, int position)
{
if(n==capacity)
return n; //cannot insert any element since exceeding capacity
int index = position-1;
for(int i=n-1; i>=index;i--)
array[i+1]=array[i];
array[index]=x;
return (n+1); //returns the size of the new array.
}
``````

# 3. Delete an element from an array.

The following function will delete any element at the position specified by the user and will return the size of the modified array after deletion.

``````int delete(int array[],int n, int x)
{
int i;
for(int i=0;i<n;i++)
if(array[i]==x)
break;
if (i==n)
return n; // the element is not present in the array
for(int j=i;j<n-1;j++)
array[j]=array[j+1];
return (n-1); // returns the size of array after deletion
}
``````

# 4. Sort an array (Ascending order).

Java has an arrays class which can be used to sort arrays by importing java.util.Arrays.

``````Arrays.sort(array);
``````

These were the basic manipulation of arrays that can get you started with. If you want to dive deeper and master them to crack technical interviews, then try to build your logic to solve the following questions:

-> How to reverse an array?
-> How to find leaders in an array?
-> How to left rotate an array?