-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaArray07.txt
More file actions
41 lines (38 loc) · 1.4 KB
/
JavaArray07.txt
File metadata and controls
41 lines (38 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*Write a Java program that asks the user the length of an array (N) then takes N number of doubles as elements for the array as input. First, remove the consecutive duplicate elements from the original array to form a new array. Then print the number of elements removed from the original array.
Sample Input Sample Output
N = 8 New Array: 5.2 2.7 1.0 2.7 3.5
Please enter the elements of the array: Removed elements : 3
5.2
2.7
1.0
1.0
2.7
3.5
3.5
3.5
*/
import java.util.Scanner;
public class Task07{
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
double [] arr=new double[n];
double [] arr2= new double[n];
int k=0;
int count=0;
for(int j=0;j<n;j++){
arr[j]=sc.nextDouble();}
for(int i=0;i<n;i++){
boolean flag= false;
if(i!=n-1&&arr[i]==arr[i+1]){
count++;
flag=true;}
if(!flag){
arr2[k++]=arr[i];}}
System.out.print("New array : ");
for(int l=0;l<k;l++){
System.out.print(arr2[l]+" ");}
System.out.println("");
System.out.println("Removed elements: "+ count);
}
}