Question

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 10 20 11 22 22

Array with unique values:

10 22 11 20



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

Java code with explanation:

import java.util.Scanner;

import java.util.Arrays;

public class removeDuplicates{

public static void main(String[] args) {

int n;

System.out.print("Please enter the size of the array: ");

Scanner sc = new Scanner(System.in);

n = sc.nextInt(); //reading input array size

int[] array = new int[n];

System.out.print("Please enter the " + n + " elements of the array: ");

for(int i = 0; i<n; i++)

array[i] = sc.nextInt(); //reading the elements of the array

Arrays.sort(array); //sorting the array, so that repeated elements are consecutive

int new_size = 0;

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

if (array[i] != array[i+1]){ // if elements at index i is different to it's next element, then, considering the element

array[new_size++] = array[i];

}

}

array[new_size++] = array[n-1];

for(int i = 0; i<new_size; i++) //printing the elements

System.out.println(array[i]);

}

}

Screenshot of the code's output:

File Edit Selection View Go Debug Terminal Help removeDuplicates,java-Visual Studio Code removeDuplicates.java x 1 import java.util.scanner; 2 import java.util.Arrays; 4 public class removeDuplicatest public static void main(String[] args) [ int n System.out.print(Please enter the size of the array: Scanner scnew Scanner (System.in); nsc.nextInt) //reading input array size intl array new int[n]: System.out.print(Please enter the +n+ elements of the array:) 10 12 array[i] sc.nextInt) //reading the elements of the array 14 15 16 Arrays.sort(array); //sorting the array, so that repeated ele manoj@manoj-Inspiron-3542: File Edit View Search Terminal Help manoiananoi-Inspiron-3542: S nanoj@nanoj-Inspiron-3542 :#5 java removeDuplicates Please enter the size of the array: 9 Please enter the 9 elements of the array: 105 123 -921 -1 123 8 88 -921 int new size = θ javac renoveDuplicates.java for (int i-0; i<n-l; i++)1 19 20 21 if (array[1] != array[1+1)){ // if elements at index i array[new_size++array[i] 921 23 24 25 26 array[new_size++array [n-1]; 105 123 nanoj@nanoj-Inspiron-3542 :#5 java renoveDuplicates Please enter the size of the array: 7 Please enter the 7 ele ents of the array: 10 22 10 20 11 22 22 10 for(int i ; inew_size; i++) //printing the elements System.out.println (array[i]); 28 29 manoj@nanoj-Inspiron-3542 * 0 A 0 20% starting Java Language Server

PLEASE UPVOTE IF THE ANSWER WAS HELPFUL!!

Add a comment
Know the answer?
Add Answer to:
Write a java program to remove duplicate elements from an array. Do not use Java library...
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
  • Change (103) to base 2 in binary number? Question2 outputs) Wr difference, the product, the average,...

    Change (103) to base 2 in binary number? Question2 outputs) Wr difference, the product, the average, the distance (assuming the two entered numbers are x,y coordinates) from the origin, the maximum (the larger of the two integers), the minimum (smaller of the two integers). 2: (section 350; optional for (001/002) (18 points: 4 points for inputs, 2 points for each ite a Java program that accepts two integers from the user and then prints the sum, the Your console output...

  • 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

  • Java Programming Use a one-dimensional array to solve the following program. Write an application that inputs...

    Java Programming Use a one-dimensional array to solve the following program. Write an application that inputs six numbers [Keep the numbers in an array]. The numbers must be between 20 and 200, inclusive. If the number is not between 20 and 200, ask for another input. When a number is entered, display the number only if it is not a duplicate of a number already entered. If it is a duplicate of a number already entered, display a message that...

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

  • Write a Java program that does the following: 1. Creates a string array called firstNames that...

    Write a Java program that does the following: 1. Creates a string array called firstNames that holds these values "John", "Frank", "Nick", "Amanda", "Brittany", "Amy", "Deborah", and "Stirling". 2. Creates another string array called lastNames that holds these values "Thompson", "Smith", "Jones", "Keribarian", "Lu", "Banafsheh", "Spielberg", "Jordan", "Castle" and "Simpson". 3. Now add a method that creates an array list called randomNames which picks a random first name from the array in step 1 and a random last name in...

  • java pseudocode and source code help? Write a program that uses an array of high temperatures...

    java pseudocode and source code help? Write a program that uses an array of high temperatures for your hometown from last week (Sunday - Saturday). Write methods to calculate and return the lowest high temperature (minimum) and a method to calculate and return the highest high temperature (maximum) in the array. You must write YOUR ORIGINAL methods for minimum and maximum. You MAY NOT use Math class methods or other library methods. Write an additional method that accepts the array...

  • Write a single program in java using only do/while loops for counters(do not use array pls)...

    Write a single program in java using only do/while loops for counters(do not use array pls) for the majority of the program and that asks a user to enter a integer and also asks if you have any more input (yes or no) after each input if yes cycle again, if not the program must do all the following 4 things at the end of the program once the user is finished inputting all inputs... a.The smallest and largest of...

  • ****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 a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

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

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
ADVERTISEMENT