preloader
  • Home
  • Array Programs

All Array Programs are covered with Codes

Array Programs

  • COURSES

    03 Month

  • DURATION

    03 Hours

Integer Array Sorting using loop

import java.util.*;
            public class ArraySorting {
              public static void main(String[] args) {
                int array[] = {10,8,14,23,18};
                int length = array.length;
                for (int i = 0; i < array.length; i++) {
                  int temp = 0;
                  for (int j = i + 1; j < array.length; j++) {
                    if (array[i] > array[j]) {
                      temp = array[i];
                      array[i] = array[j];
                      array[j] = temp;
                    }
                  }
                  System.out.print(array[i] + " ");
                }
              }
            }

Integer Array Sorting and duplicate removal without loop(Collection)

import java.util.*;
                public class Test {
                   public static void main(String[] args) {
                      int arr[]= {10,30,20,40,50,10,40,50,60,68};
                      Set st = new TreeSet();
                      for (int i = 0; i < arr.length; i++) {
                         st.add(arr[i]);
                      }
                      System.out.println(st);
                   }
                }

Program to print alternate shorting Array

import java.util.*;
            public class AlternateSort {
              public static void main(String[] args) {
                 int arr[] = {3, 7, 5, 9, 10,54,30};
                int n = arr.length;
                alternateSortmethod(arr, n);
              }
              private static void alternateSortmethod(int ar[], int n) {
                Arrays.sort(ar);
                int i = 0;
                int j = n - 1;
                while (i < j) {
                  System.out.print(ar[i++] + " ");
                  System.out.print(ar[j--] + " ");
                }
                if (n % 2 != 0) {
                  System.out.print(ar[i]);
                }
              }
            }

Program to print 2nd highest element from array

import java.util.*;
            public class Test {
              public static void main(String[] args) {
                int arr []= {12,1,3,11,5,7,8};
                int length = arr.length;
                for (int i = 0; i < length; i++) {
                  int temp = 0;
                  for (int j = i + 1; j < length; j++) {
                    if (arr[i] > arr[j]) {
                      temp = arr[i];
                      arr[i] = arr[j];
                      arr[j] = temp;
                    }
                  }
                }
                System.out.println("2nd highest element = " +
                  arr[length - 2]);
              }
            }

print the pairs from array having sum n

import java.util.*;
            public class Test {
              public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println(" Enter the sum ");
                int sum = sc.nextInt();
                int arr []= {12,1,2,11,6,7,8,5};
                LinkedHashSet < Integer > st =
                  new LinkedHashSet < Integer > ();
                for (int i = 0; i < arr.length; i++) {
                  int temp = sum - arr[i];
                  if (st.contains(temp)) {
                    System.out.println(" the pairs are " + "[" + arr[i] +
                      " , " + temp + "]");
                  }
                  st.add(arr[i]);
                }
              }
            }

Reverse Array using Loop

import java.util.*;
            public class RevArr {
              public static void main(String[] args) {
                 int arr[ ]={10,30,20,40,50};
                int length = arr.length();
                for (int i = length - 1; i >= 0; i--) {
                  System.out.println(arr[i] + " ");
                }
              }
            }

Program to merge two array ,sort it and remove duplicate from it without sort method

import java.util.*;
                public class Test {
                   public static void main(String[] args) {
                      int a[]= {4,5,7,8,13,22};
                      int b[]= {24,13,28,30,29};
                
                      //merge array
                      int arr[] = mergeArr(a, b);
                      //short array
                      for (int i = 0; i < arr.length; i++) {
                         int temp = 0;
                         for (int j = i + 1; j < arr.length; j++) {
                            if (arr[i] > arr[j]) {
                               temp = arr[i];
                               arr[i] = arr[j];
                               arr[j] = temp;
                            }
                         }
                      }
                      //remove duplicate
                      int len = arr.length;
                      int j = 0;
                      for (int i = 0; i < len - 1; i++)
                         if (arr[i] != arr[i + 1]) {
                            arr[j++] = arr[i];
                         }
                   }
                   arr[j++] = arr[len - 1];
                   for (int k = 0; k < j; k++) {
                      System.out.print(arr[k] + " ");
                   }
                }
                static int[] mergeArr(int[] a, int[] b) {
                   int m = a.length;
                   int n = b.length;
                   int total[] = new int[m + n];
                
                   int i = 0;
                   int j = 0;
                   int k = 0;
                   while (i < m) {
                      total[k] = a[i];
                      i++;
                      k++;
                   }
                   while (j < n) {
                      total[k] = b[j];
                      j++;
                      k++;
                   }
                   return total;
                  }
                }

Write a program to sort an array of strings based on their lengths. If multiple words are of equal lengths then resolve the tie based on alphabetical order.

import java.util.Arrays;
                public class StringShortBasedLength {
                   public static void main(String[] args) {
                      String []arr = {"Goodboyhere", "I", "from", "am","to", "fight"};
                      int n = arr.length;
                      Arrays.sort(arr);
                      SortString(arr, n);
                      for (int i = 0; i < n; i++) {
                         System.out.print(arr[i] + " ");
                      }
                   }
                   static void SortString(String[] arr, int n) {
                      for (int i = 1; i < n; i++) {
                         String temp = arr[i];
                         int j = i - 1;
                         while (j >= 0 && temp.length() < arr[j].length()) {
                            arr[j + 1] = arr[j];
                            j--;
                         }
                         arr[j + 1] = temp;
                      }
                   }
                }

Print non duplicate elements from array from loop.

public class Test {
            public static void main(String[] args) {
               int arr []= {12,1,2,11,6,7,8,5,2,11};
               int length = arr.length;
               for (int i = 0; i < length; i++) {
                  int count = 0;
                  for (int j = 0; j < length; j++) {
                     if (arr[i] == arr[j] && i != j) {
                        count++;
                     }
                  }
                  if (count == 0) {
                     System.out.print(arr[i] + " ");
                  }
               }
            }
         }

Java program forGiven an unsorted array A of size N, find a continuous sub-array which sums up to S. For example if the input array is [ 12,7,29,6, 2, 11,4,8] and the expected sum is 8, then there are two possibilities [6,2] and [8] if the expected sum is 19 there are two possibilities [12,7] and [6,2,11]. [11,8] is not a possibility since its not continuous.(All Possible pairs of array)

import java.util.Arrays;
                public class PairSum {
                   public static void main(String[] args) {
                     int[] nums = {12,7,29,6, 2, 11,4,8};
                      int target = 19;
                      for (int i = 0; i < nums.length; i++) {
                         int sum = 0;
                         for (int j = i; j < nums.length; j++) {
                            sum = sum + nums[j];
                            if (sum == target) {
                               System.out.println(IntStream.range(i, j + 1)
                                  .mapToObj(k -> nums[k]).collect(Collectors.toList()));
                            }
                         }
                      }
                   }
                }

print only non duplicate elements from array from set

import java.util.*;
            public class Test {
               public static void main(String[] args) {
                   int arr []= {12,1,2,11,6,7,8,5,2,11};
                  int length = arr.length;
                  Set st = new HashSet();
                  for (int i = 0; i < arr.length; i++) {
                     st.add(arr[i]);
                  }
               }
               System.out.print(st);
              }
            }

Given an integer array nums and an integer k, return the kth largest element in the array.
Note:that it is the kth largest element in the sorted order, not the kth distinct element.
Input: nums = [3,2,1,5,6,4], k = 2
Output: 5
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4

import java.util.Arrays;
                import java.util.Scanner;
                public class Test {
                   public static void main(String[] args) {
                      Integer [] arr1 = new Integer [] {3,2,3,1,2,4,5,5,6};
                Integer [] arr2 = new Integer [] {3,2,1,5,6,4};
                      Scanner sc = new Scanner(System.in);
                      System.out.println("Enter the K1 value");
                      int K1 = sc.nextInt();
                      System.out.println("Enter the K2 value");
                      int K2 = sc.nextInt();
                      Arrays.sort(arr1);
                      Arrays.sort(arr2);
                      // for first array kth largest element in the sorted order
                      System.out.println("kth largest element in the sorted order = " + (arr1.length - K1));
                      //for second array kth largest element in the sorted order
                      System.out.println("kth largest element in the sorted order = " + (arr2.length - K2));
                      sc.close();
                   }
                }

Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes.
For example, in an array A:
Example :
Input: A[] = {-7, 1, 5, 2, -4, 3, 0}
Output: 3
3 is an equilibrium index, because:
A[0] + A[1] + A[2] = A[4] + A[5] + A[6];
Input: A[] = {1, 2, 3}
Output: -1;
Write a function int equilibrium(int[] arr, int n); that given a sequence arr[] of
size n, returns an equilibrium index (if any) or -1 if no equilibrium indexes exist.

public class EquilibrimNo {
                public static void main(String[] args) {
                   int arr[] = { -7, 1, 5, 2, -4, 3, 0 };
                   int arr_size = arr.length;
                   System.out.println(
                      "First equilibrium index is " +
                      equilibrium(arr, arr_size));
                }
                static int equilibrium(int[] arr, int n) {
                   int sum = 0;
                   int leftSum = 0;
                   for (int i = 0; i < n; i++) {
                      sum += arr[i];
                   }
                   for (int i = 0; i < n; ++i) {
                      sum -= arr[i];
                      if (leftSum == sum) {
                         return i;
                      }
                      leftSum += arr[i];
                   }
                   return -1;
                }
             }

Program to find the no of occurence each no in integer array

import java.util.Arrays;
            import java.util.List;
            import java.util.Map;
            import java.util.stream.Collectors;
            public class MainClass {
               public static void main(String[] args) {
                  List < Integer > numsList = Arrays.asList(10, 20, 30, 35, 25, 29, 30, 45, 38, 10, 20, 30);
                  Map < Integer, Long >
                     map = numsList.stream().collect(Collectors.groupingBy(Integer::intValue, Collectors.countin g()));
                  map.forEach((k, v) -> {
                     System.out.println(k + " = " + v);
                  });
               }
            }

Program to find total count of no of pair whose difference is 2

import java.util.*;
            public class ArrayRemoveDuplicate {
               public static void main(String[] args) {
                  int arr[] = {10,2,13,5,17,9,6,19};
                  int def = 8;
                  int length = arr.length;
                  HashSet < Integer > hs = new LinkedHashSet < Integer > ();
                  int count = 0;
                  for (int i = 0; i < length; i++) {
                     int temp = arr[i] + def;
                     if (hs.contains(temp)) {
                        count++;
                     }
                     hs.add(arr[i]);
                  }
                  System.out.println(count);
               }
            }

Related Course

course thumb

OOP Concept

Inheritance,Polimorphism,Abstraction and Encapsulation

Visit
course thumb

Exception handling

Exception handling ,it type ,error vs Exception

Visit
course thumb

All Topics Questions

This section we will cover all topic wise java question

Visit