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
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](http://img.homeworklib.com/questions/93ebf9c0-d66a-11ea-ac0b-99e731b3c273.png?x-oss-process=image/resize,w_560)
PLEASE UPVOTE IF THE ANSWER WAS HELPFUL!!
Write a java program to remove duplicate elements from an array. Do not use Java library...
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 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
(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...
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. 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 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 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) 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 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 to input a number of values into an array and then process the array by passing it to various methods that will compute and return information we are interested in. These include: 1. the average of the values in the array 2. the standard deviation of the values in the array 3. the number of items in the array less than the average 4. whether the array’s values are in increasing order or not First, you...