Question

Write a Java program to remove the duplicate elements of a given array and return the...

Write a Java program to remove the duplicate elements of a given array and return
the new length of the array.
Sample array: [20, 20, 32, 76, 30, 40, 50, 50, 52]
After removing the duplicate elements the program should return 6 as the new length of the
array.
Out put
Original array length: 9
Array elements are: 20 20 32 76 30 40 50 50 52
The new length of the array is: 7

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.*;

class Program{

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter length of input array: ");

        int arrlen = sc.nextInt();

        int[] arr = new int[arrlen];

        System.out.println("Enter the elements of array: ");

        for(int i = 0;i<arrlen;i++){

            arr[i] = sc.nextInt();

        }

        System.out.print("\nOriginal array length: "+arrlen);

        System.out.print("\nArray elements are: ");

        for(int i = 0;i<arrlen;i++){

            System.out.print(arr[i]);

        }

        for(int i = 0;i<arrlen-1;i++){

            for(int j = i+1; j<arrlen ;j++){

                if(arr[i]==arr[j]){             //comparing adjacent element for equality

                    arr[j]=arr[arrlen-1];       //when duplicate element found, replace it

                    arrlen--;                   //with last element, and decrease arrlen by 1

                }

            }

        }

        System.out.print("\nThe new length of the array is: "+arrlen);        

    }

}

Add a comment
Know the answer?
Add Answer to:
Write a Java program to remove the duplicate elements of a given array and return the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a java program to remove duplicate elements from an array. Do not use Java library...

    Write a java program to remove duplicate elements from an array. Do not use Java library utilities or 3rd party tools to solve this problem. You need to use the “for loop” and manage the index as you traverse thru the array [i ] while looking for duplicates. For example: Example 1 --------------------- Original Array: 105 123 -921 -1 123 8 8 8 -921 Array with unique values: 105 123 -921 -1 8 Example 2 --------------------------- Original Array: 10 22...

  • Write a program to remove all duplicate elements from a tuple and create a new tuple....

    Write a program to remove all duplicate elements from a tuple and create a new tuple. Print the newly created tuple. If tuple is (10, 15, 8, 5,4,10,8,4,3,5) then the new tuple will be (10,15,8,5,4,3)

  • Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges...

    Assignment 6, Merge Arrays (java) Instructions In this assignment, you will write a program which merges two arrays of positive integers and removes any duplicate entries. Your program will first ask for a valid length which must be an integer which is 10 or greater. The program should continue to ask until a valid length is entered. The program will then create two arrays of the length entered, fill these with random integers between 1 and 100 inclusive, and print...

  • 1. (100 pts) Write a program that swaps the elements of an array pairwise. Start from...

    1. (100 pts) Write a program that swaps the elements of an array pairwise. Start from the left of the array, take 2 elements at a time and swap the two elements. Continue in this fashion until you reach the end of the array. Declare an integer array of size 10 and place random numbers in the range [0?9]. Print the original array, pairwise swap the elements of the array and print the final array after swap. Consider the following...

  • Write a program in Java that stores an array nxm  in less space than nxm  given...

    Write a program in Java that stores an array nxm  in less space than nxm  given that the numbers in the area are repeated a lot     - Input:      Array n x m    - Output:         - Area is stored in less than n x m space         - When called the program is return the original array          Example:   Given 3x6 array A           A: [2,3,5,5,5,5                   1,3,5,6,5,6                   0,5,5,5,6,3] Program stored it  in less that   When...

  • 23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a...

    23.1 Append to Oversize Array Java Help Given an oversize array with size1 elements and a second oversize array with size2 elements, write a method that returns the first array with the elements of the second appended to the end. If the capacity of the oversize array is not large enough to append all of the elements, append as many as will fit. Hint: Do not construct a new array. Instead, modify the contents of the oversize array inside the...

  • ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of...

    ****WRITE A JAVA PROGRAM THAT : Write a method named stretch that accepts an array of integers (that the user inputs) as a parameter and returns a new array twice as large as the original, replacing every integer from the original array with a pair of integers, each half the original. If a number in the original array is odd, then the first number in the new pair should be one higher than the second so that the sum equals...

  • Write array methods that carry out the following tasks for an array of integers by completing...

    Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods class below. For each method, provide a test program. public class ArrayMethods { private int[] values; public ArrayMethods(int[] initialValues) { values = initialValues; } public void swapFirstAndLast() { . . . } public void shiftRight() { . . . } .. . } a. Swap the first and last elements in the array. b. Shift all elements to the right by one...

  • Please write a Java program: Given an array of positive integers, return a count of the...

    Please write a Java program: Given an array of positive integers, return a count of the number of even integers. countEvens([2, 3, 5])-1 countEvens([4, 20]) - 2 countEvens([3, 7, 1, 11]) 0 Go Save, Compile, Run (ctrl-enter) int countEvens (int[] nums) {

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT