Skip to content

Commit 4b3b93c

Browse files
authored
Add files via upload
1 parent a4257b5 commit 4b3b93c

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

JavaArray07.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*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.
2+
3+
Sample Input Sample Output
4+
N = 8 New Array: 5.2 2.7 1.0 2.7 3.5
5+
Please enter the elements of the array: Removed elements : 3
6+
5.2
7+
2.7
8+
1.0
9+
1.0
10+
2.7
11+
3.5
12+
3.5
13+
3.5
14+
*/
15+
16+
import java.util.Scanner;
17+
public class Task07{
18+
public static void main(String [] args){
19+
Scanner sc=new Scanner(System.in);
20+
int n=sc.nextInt();
21+
double [] arr=new double[n];
22+
double [] arr2= new double[n];
23+
int k=0;
24+
int count=0;
25+
for(int j=0;j<n;j++){
26+
arr[j]=sc.nextDouble();}
27+
for(int i=0;i<n;i++){
28+
boolean flag= false;
29+
if(i!=n-1&&arr[i]==arr[i+1]){
30+
count++;
31+
flag=true;}
32+
if(!flag){
33+
arr2[k++]=arr[i];}}
34+
System.out.print("New array : ");
35+
for(int l=0;l<k;l++){
36+
System.out.print(arr2[l]+" ");}
37+
System.out.println("");
38+
System.out.println("Removed elements: "+ count);
39+
}
40+
}
41+

0 commit comments

Comments
 (0)