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...

  • Write a Java program to find the duplicate values of an array of integer values. import...

    Write a Java program to find the duplicate values of an array of integer values. import java.util.Arrays; public class ex7DuplicateValue { public static void main(String[] args) { int[] my_array = {1, 2, 3, 3, 4, 5, 6, 2}; } } //Duplicate Element : 2 //Duplicate Element : 3

  • 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...

  • (Remove duplicates) Write a method that removes the duplicate elements from an array list of integers...

    (Remove duplicates) Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList < Integer > list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers in their input order separated by exactly one space. Sample Run 1 Enter ten integers: 34 5 3 5 6 4 33 2 2 4 The distinct integers are 34 5 3...

  • 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...

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