WAP to remove duplicate elements in an array

WAP to remove duplicate elements in an array: Here’s an example program in Java to remove duplicate elements in an array:

import java.util.Arrays;

public class RemoveDuplicates {
    
    public static void main(String[] args) {
        int[] arr = { 1, 2, 3, 2, 4, 5, 4, 6, 7, 6 };
        int[] result = removeDuplicates(arr);
        System.out.println("Original Array: " + Arrays.toString(arr));
        System.out.println("Array after removing duplicates: " + Arrays.toString(result));
    }

    public static int[] removeDuplicates(int[] arr) {
        int[] temp = new int[arr.length];
        int j = 0;

        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] != arr[i + 1]) {
                temp[j++] = arr[i];
            }
        }

        temp[j++] = arr[arr.length - 1];
        int[] result = new int[j];

        for (int i = 0; i < j; i++) {
            result[i] = temp[i];
        }

        return result;
    }
}

Output:

Original Array: [1, 2, 3, 2, 4, 5, 4, 6, 7, 6]
Array after removing duplicates: [1, 2, 3, 4, 5, 6, 7]

Explanation:

  1. We create a temporary array of the same length as the original array.
  2. We iterate through the original array and compare each element with its adjacent element.
  3. If the elements are not equal, we add the element to the temporary array.
  4. After the loop, we add the last element to the temporary array.
  5. We create a new array of the size equal to the number of non-duplicate elements.
  6. We copy the non-duplicate elements from the temporary array to the new array.
  7. We return the new array containing only the non-duplicate elements.

Core Java Related Article –

Array interview coding questions in java

  1. Write a program to find the maximum and minimum elements in an array in Java.
  2. Implement a function that checks whether a given value is present in an array or not.
  3. Write a program to sort an array in ascending and descending order in Java.
  4. Implement a function that returns the second largest element in an array.
  5. Write a program to find the sum of all the elements in an array in Java.
  6. Implement a function that returns the intersection of two arrays.
  7. Write a program to rotate an array in Java by a given number of positions.
  8. Implement a function that finds the kth smallest element in an unsorted array.
  9. Write a program to merge two sorted arrays in Java.
  10. Implement a function that removes a given element from an array and returns the new array.

These are just a few examples of array programming interview questions in Java. There are many more variations of these questions and other array manipulation problems that may be asked in interviews. It is important to have a good understanding of Java’s array data type and its methods to solve these problems efficiently.

Array in java explain

In Java, an array is an object that stores a fixed-size collection of elements of the same type. Each element in the array is identified by an index or a subscript that is used to access it.

Declaring an array in Java involves specifying the type of the elements that the array will hold, followed by square brackets that indicate the array’s size. For example, the following code declares an array of integers with ten elements:

int[] myArray = new int[10];

This creates an array of integers with ten elements, all initialized to zero. The size of the array cannot be changed once it is created.

You can also declare and initialize an array in one statement, like this:

Array Important Coding Question

Array interview questions in java

Here are some common interview questions related to arrays in Java:

  1. What is an array in Java?
  • An array in Java is an object that holds a fixed-size collection of elements of the same type.
  1. How do you declare an array in Java?
  • To declare an array in Java, you specify the type of the elements that the array will hold, followed by square brackets that indicate the array’s size. For example: int[] myArray = new int[10];.
  1. How do you access an element of an array in Java?
  • You access an element of an array in Java using the index of the element in square brackets. For example: int firstElement = myArray[0];.
  1. What is the difference between an array and an ArrayList in Java?
  • An array has a fixed size that cannot be changed once it is created, while an ArrayList can grow and shrink dynamically. Also, an array can hold only elements of the same type, while an ArrayList can hold elements of different types.
  1. How do you iterate over an array in Java?
  • You can iterate over an array in Java using a loop, such as a for loop or a foreach loop. For example:
for (int i = 0; i < myArray.length; i++) {
    // do something with myArray[i]
}
  1. Can you have an array of objects in Java?
  • Yes, you can have an array of objects in Java. For example: Person[] people = new Person[10];.
  1. What is the difference between a one-dimensional and a two-dimensional array in Java?
  • A one-dimensional array has a single index, while a two-dimensional array has two indices. A two-dimensional array can be thought of as an array of arrays.
  1. How do you sort an array in Java?
  • You can sort an array in Java using the Arrays.sort() method. For example: Arrays.sort(myArray);.
  1. How do you find the maximum or minimum element in an array in Java?
  • You can find the maximum or minimum element in an array in Java using a loop and comparing each element to the current maximum or minimum. Alternatively, you can use the Arrays.stream() method to convert the array to a stream and then use the max() or min() method to find the maximum or minimum element. For example: int max = Arrays.stream(myArray).max().getAsInt();.

Leave a Comment