This is a 2 part question 1st the question asks to create a method to check whether two sequences have the same values in the same order.
Secondly the bit I'm stuck on the question asks to create a method to check whether two sequences have the same values in some order, ignoring duplicates ie Sequence 1 (1, 4, 9, 16, 9, 7, 4, 9, 11) Sequence 2 (11, 11, 7, 9, 16, 4, 1) So sequence 1 would still be identical with sequence 2 as 11, 4, and 9 are duplicate elements
So I added a method public boolean equals(Sequence other) that checks whether the two sequences have the same values in the same order part 1, but what I need to do now is part 2 check whether two sequences have the same values in some order, ignoring duplicates.
import java.util.Arrays;
public class Sequence {
private int[] values;
public Sequence(int size)
{
values = new int[size];
}
public void set(int i, int n)
{
values[i] = n;
}
public boolean equals(Sequence obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
Sequence other = (Sequence) obj;
if (!Arrays.equals(values, other.values))
{
return false;
}
return true;
}
public static void main(String args[]){
Sequence s = new Sequence(5);
Sequence s2 = new Sequence(5);// new Sequence(4)
s.set(0, 1);
s2.set(0, 1);
System.out.println(s.equals(s2));//will print true
}
}
I'm a little confused I know this is how you check duplicates but that is all I know I don't really know how use it in this scenario or how to ignore duplicates
for (int i = 0; i < values.length; i++)
{
for (int j = i + 1; j < other.length; j++)
{
if (values[i].equals(other[j]) ) {}
}
}