Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

No Twins?


No Twins?
Write a program that accepts as input a vector and returns the same vector, but retaining only the first occurrence of an element whenever there is consecutive repetition of this element.

Example:

 in = [1 2 2 2 3 4 4 3 5]
 out= [1 2 3 4 3 5]

in= [1 1 2 2 2 3 4 4 1 1];
out = [1 2 3 4 1];

MATLAB CODE:



clc

clear all
close all
Y=[];
x=input('Enter the array:');
m=x(1);
Y=[Y m];
for i=2:length(x)
    if(m~=x(i))
          m=x(i);
        Y=[Y m];
      
    end
end

Explanation:


Sample Output:







JAVA CODE:

import java.util.Scanner;
class Twin
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.println("Enter the length of the array:");
int l=obj.nextInt();
int a[]=new int[l];
for(int i=0;i<l;i++)
{
System.out.println("Enter the number at"+i+" th position");
a[i]=obj.nextInt();
}
int m=a[0];
System.out.println("The output is:");
System.out.print(m+" ");
for(int i=1;i<l;i++)
{
if(m!=a[i])
{
m=a[i];
System.out.print(m+" ");
}
}
}
}

Sample OUTPUT:



No comments

Popular Posts

Followers