Display elements without redundancy in an array - java

I made this code. I think it is wrong.
public void display() {
for (int i = 0; i < tabT.length; i++)
if (tabT[i] != null)
for (int j = 0; j <= i; j++)
if (tabT[i] != tabT[j])
System.out.println(tabT[i].getCar());
}
How do I display elements without redundancy in an array?

If you want to use only arrays, you can do something like this:
Make an temp (helper) array, which will include each element seen so far in tabT. Then, before printing the value, you check if it's not appearing in the helper array (tmp).
For example, if you have values in tabT, and you don't want to print each one more than once:
int[] tabT = {1,2,3,1,1,2,6,7,2,7,1};
int[] tmp = new int[tabT.length];
boolean flag;
for (int i = 0; i < tabT.length; i++) {
tmp[i] = tabT[i];
flag = true;
for (int j = 0; j < tmp.length; j++)
if (tabT[i] == tmp[j] && i!=j) {
flag = false;
}
if(flag)
System.out.println(tabT[i]);
}
Output: [1,2,3,6,7]
You can easily apply this idea to your program, and you'll have each element printed only once:
Cars[] tmp = new Cars[tabT.length]; //Assuming tabT is from type Cars[]
boolean flag = true;
for (int i = 0; i < tabT.length; i++) {
tmp[i] = tabT[i];
if (tabT[i] != null) {
for (int j = 0; j < tmp.length; j++)
if (tabT[i].getCar().equals(tabT[j].getCar()) && i!=j)
flag = false;
if(flag)
System.out.println(tabT[i].getCar());
}
}
This will print each car (or whatever you're printing) only once.

Objects compared via equals() For example
if (!tabT[i].equals(tabT[j]))
you are comparing the references values not the objects
for (int i=0; i< tabT.length; i++) {
boolean f = false;
for (int j=i+1; j <tabT.length; j++)
if (tabT[i].equals(tabT[j])) {
f=true;
break;
}
if (!f)
System.out.println(tabT[i].getCar());
}
this should give you all combinations non-repeating for i and j, so we don't compare them multiple times.

== and != test equality at the object level (i.e. if both instances are the same). What you need is to compare the value represented by each object (e.g. if two strings are equals), then you need to ask whether !tabT[i].equals(tabT[j]), and make the elements of tabT implement equals).
Or convert the array to a set, which removes duplicates.
T[] tabT = ...
Set<T> set = new LinkedHashSet<T>(Arrays.asList(tabT))
for (T t:set) System.out.println(t);
I used a LinkedHashSet because it preserves the order of the elements in the array. Note that you need to implement equals and hashcode.

Put tabT array in a Set. There will be no duplicate items.
Set tabTList = new HashMap(Listjava.util.Arrays.asList(tabT);

Why not trying something like this? (I'm supposing that you are working with String types)
HashSet<String> hashSet = new HashSet<String>();
for (int i = 0; i < tabT.length; i++) {
hashSet.add(tabT[i]);
}
You can't have duplicates into a set, so now you can iterate the set to get the uniques.
java.util.Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()) {
System.out.println((String)iterator.next());
}

You could consider a HashMap if you do want to keep track of duplicate count. Iterate through array once to place objects into the HashMap with their respective counts. Then iterate through the array again checking against the HashMap. This would be O(n) time as opposed to potential O(n^2)

Related

Java - iterate through ArrayList for only increasing elements

Here is my ArrayList:
[1,2,1,0,3,4]
I'm trying to return this:
[1,2,3,4]
Here is my current attempt:
for (int i = 0; i < myArray.size() - 1; i++) {
if (myArray.get(i) < myArray.get(i + 1)) {
System.out.println("Increasing sequence...");
}
}
However, this is not returning the desired output, any ideas?
You'll have to maintain an index (or value) of the last element that you had printed and store it in some variable. Then, you'll have to use the stored element for every new element and check if is greater than the stored element.
As you have mentioned, the first element has to be anyway printed, no matter what.
Something like this might work:
List<Integer> myArray = Arrays.asList(new Integer[]{1,2,1,0,3,4});
System.out.println(myArray.get(0));
int prevPrint = myArray.get(0);
for (int i = 1; i < myArray.size();i++) {
if (myArray.get(i) > prevPrint) {
System.out.println(myArray.get(i));
prevPrint = myArray.get(i);
}
}
The reason why your program was failing was because you were comparing the adjacent two values only and it was possible that you might have already printed a value which is greater than any of the two adjacent values.
A similar question, but a totally different approach (LIS) exists and can be found here
A slight variant on Parijat's answer to avoid repeating the System.out.println:
for (int j = 0; j < myArray.size();) {
System.out.println(myArray.get(j));
int start = j;
do {
++j;
while (j < myArray.size() && myArray.get(j) <= myArray.get(start));
}
Try this,
public static List<Integer> findIncreasingOrder(int[] nums) {
List<Integer> result = new ArrayList<>();
int MAX = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
int value = nums[i];
if (value >MAX){
System.out.println(value);
MAX = value;
result.add(value);
}
}
return result;
}

Not able to add to ArrayList properly

I have 2 methods in my program, one to add ***** above and below the smallest int in the array and one to add %%%%% above and below the largest. The method for the largest is essentially the same as the other but for some reason isn't adding what is needed.
Here is the smallest element method:
public static ArrayList smallestElement() {
int smallest = array[0];
for (int i = 0; i < array.length; i++)
if (array[i] < smallest)
smallest = array[i];
String smallestString = String.valueOf(smallest);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < array.length; i++) {
if (smallestString.equals(String.valueOf(array[i]))) {
list.add("*****");
list.add(Integer.toString(array[i]));
list.add("*****");
} else {
list.add(Integer.toString(array[i]));
}
}
return list;
}
Here is the method for the largest element:
public static ArrayList largestElement() {
int largest = array[0];
for (int i = 0; i < array.length; i++)
if (array[i] > largest)
largest = array[i];
String largestString = String.valueOf(largest);
for(int i = 0; i < array.length; i++) {
if (largestString.equals(String.valueOf(array[i]))) {
smallestElement().add("%%%%%");
smallestElement().add(Integer.toString(array[i]));
smallestElement().add("%%%%%");
} else {
smallestElement().add(Integer.toString(array[i]));
}
}
System.out.println(smallestElement());
return smallestElement();
}
}
If anyone knows why this isn't performing correctly, I would really appreciate the help
You are creating a new object every time you are executing the smallestElement function. Instead do something like,
ArrayList<String> list = smallestElement();
Then use this list object every time you are calling smallestElement() method
You have already created the list 3 times over by this line
smallestElement().add("%%%%%");
smallestElement().add(Integer.toString(array[i]));
smallestElement().add("%%%%%");
Create just 1 list and use it instead of calling the smallestelementelement() function multiple times
You are overcomplicating things here. There is no need to turn that minimum array value into a string right there (and to then do String comparisons later on). Btw: those string comparisons are also your problem: your code will definitely not work when your minimal value shows up several times in your array - because your code will put in those patterns for each match!
Instead, you could do something like:
int indexToUse = 0;
for (int i = 0; i < array.length; i++) { // please always use braces!
if (array[i] < array[indexToUse]) {
indexToUse = i;
}
}
List<String> valuesWithMarkerStrings = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
if (i == indexToUse -1 || i == indexToUse+1) {
valuesWithMarkerStrings.add("******");
} else {
valuesWithMarkerStrings.add(Integer.toString(array[i]);
}
}
(where my solution assumes that you want to have *** ... instead of array[i] for such rows ... )

How to remove an Integer from an Array Java

I am a newbie in Java programming and I have a trouble in my program.
I have an array and i want to extract the max integer from it and return it to the main program. But without using ArrayList.
I must not have 0 in this certain position when i print it so I cant just replace it. Take a look at what i did so far but i think that it is so wrong.
public int extractMax()
{
for (int i = 0; i < size; i++)
{
if(maxInsert == Queue[i])
{
Queue[i] = null;
return maxInsert;
}
} return -1;
}
You cannot assign null if the array in a an array of primitives, you would see an error like this :
cannot convert from null to int
If the array is an array of objects (Integer for example), then assigning it to null would work, but I would suggest if you need to manipulate the entries of your array that you use a List instead.
For example :
List<Integer> integers = new ArrayList<>(Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9,10}));
System.out.println(integers.size());
integers.remove(new Integer(3));
System.out.println(integers.size());
Would print :
10
9
So the logic here is to basically iterate through every element of the array to see if there is an element greater than the current greatest element (starts at the first element) in the array. If a greater element is found, then the max variable is reset to this new value and the loop carries on. If an even greater element is found, then the same thing happens by updating the max variable. To actually remove this element from the array, you can create a new array with a size of one less than the original size, copy all the elements up to the max into the new array, and shift all the elements to the right of the max by to the left by one. This is what it should look like.
public int extractMax()
{
maxInsert = Queue[0];
int maxIndex = 0;
for (int i = 1; i < size; i++)//get the location of the max element
{
if(maxInsert < Queue[i])
{
maxInsert = Queue[i];
maxIndex = i;
}
}
int[] temp = new int[Queue.length - 1];
for(int j = 0; j < maxIndex; j++){ //adding elements up until the max
temp[j] = Queue[j];
}
for(int k = maxIndex; k < temp.length; k++){ //adding elements after the max
temp[k] = Queue[k+1];
}
Queue = temp; //queue is now this new array we've just made
return maxInsert;
}
Basically you can not remove an element from an Array like in List objects. So create a new Array and add all elements in Queue except the bigger one to the new Array. And lastly, assign the new array to the Queue. Here is an example code:
public int extractMax()
{
for (int i = 0; i < size; i++)
{
if(maxInsert == Queue[i])
{
removeFromArray(i);
return maxInsert;
}
} return -1;
}
private void removeFromArray(int i) {
int[] newQueue = new int[Queue.length-1];
int k = 0;
for (int j = 0; j < newQueue.length; j++,k++) {
if(i == j) {
k++;
}
newQueue[j] = Queue[k];
}
Queue = newQueue;
}

Removing duplicate strings from an array?

How can I remove duplicate strings from a string array without using a HashSet?
I try to use loops, but the words not delete.
StringBuffer outString = new StringBuffer("Our, aim, and, isn't, easy, you, you're, actual, and, are, aren't, and, improve, achieving, and, Obviously, and, illumination, are");
wordList = outString.toString().split(", ");
for (i = 0; i < wordList.length; i++) {
for (j = 0; j < wordList.length; j++) {
if((wordList[i]!=wordList[j])&&(j>i)){
t=true;
}
}
if(t==true){
k++;
}
}
String[] wordList1 = new String[k];
wordList = outString.toString().split(", ");
for (i = 0; i < wordList.length; i++) {
(j = 0; j < wordList.length; j++) {
if((wordList[i]!=wordList[j])&&(j>i)){
t=true;
}
}
if(t==true){
wordList1[i]=wordList[i];
}
}
1)
I think you need to use the equals operator. Try
if (!wordList[i].equals(wordList[j])){
instead of !=.
2) Also Kevin is right. You need to set t back to false.
3) Side note as pointed out by others already: To be more efficient you should start the inner loop with
for (j = i+1; j < wordList.length; j++) {
4) Another side note: Your result array is still too long. If you don't want to use a List<String> and it is ok to loose the original array you could go with a solution as suggested by Zim-Zam O'Pootertoot and set the original duplicates to null, add a counter
to count how many null values you assigned, initialize the new array with the correct size and loop a final time over the first array and copy only the non-null values into your new array.
Try this code to remove dup words:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < wordList.length; i++) {
boolean found = false;
for (int j = i+1; j < wordList.length; j++) {
if (wordList[j].equals(wordList[i])) {
found = true;
break;
}
}
// System.out.printf("Checking: [%s]%n", wordList[i]);
if (!found) {
if (sb.length() > 0)
sb.append(' ');
sb.append(wordList[i]);
}
}
System.out.printf("Unique: [%s]%n", sb);
If you are allowed to use Lists, you can define a generic method that does this fairly easily:
public <T> T[] removeDuplicates(final T[] array) {
List<T> noDuplicates = new ArrayList<T>();
for (T arrayElem : array) {
if (!noDuplicates.contains(arrayElem)) {
noDuplicates.add(arrayElem);
}
}
return (T[]) noDuplicates.toArray();
}
You probably want to set t back to false after pulling the value you want:
if(t)
{
wordList1[i]=wordList[i];
t = false;
}
Also this:
if((wordList[i]!=wordList[j])&&(j>i))
Will always return true since strings are immutable (unless you compared a string to an exact reference of itself which you disallow with j>i). You need to change it to say this:
if (!(wordList[i].equals(wordList[j]))&&(j>i))
Using .equals will compared that they contain the same string, not that they point to the exact reference of a string.
Not sure if that's the only problems or not, a bit unclear from what's given.
In your inner loop, initialize j = i + 1
if(wordlist[i] != null && wordlist[i].equals(worldlist[j])) { wordlist[j] = null; }
...and then compact the array when you're finished to remove all null values
How about using a List:
wordList = outString.toString().split(", ");
List<String> finalList = new ArrayList<String>();
for(String val : wordList) {
if(!finalList.contains(val)) {
finalList.add(val);
}
}
A Set would be more efficient, however. If you can't use a List or a Set, and you are forced to remove the duplicates, then you will have to loop through the array each time, which will perform horribly.
Iterate through the array, and store in an auxiliary int[] or List<Integer> the indexes of duplicates that you find with your two for's.
Create a new Array, with size equal to the original one minus the size of the repeated Strings.
Iterate through your original array, if the index isn't on your auxiliary list, set it to your new Array.
The best and most effective method is to suppose arr is the array that contains strings and can have duplicate values:
Arrays.sort(arr);
int l = 0;
for (int a = 0; a < arr.length; a++) {
if (a == arr.length - 1)
l++;// its a unique value
else if (!(a[a + 1].equals(arr[a])))
l++;// its also a unique
}
String newArray[] = new String[l];
l = 0;
for (int a = 0; a < arr.length; a++) {
if (a == arr.length - 1)
newArray[l] = arr[a];
else if (!(a[a + 1].equals(arr[a]))) {
newArray[l] = arr[a];
l++;
}
}
Try this...
public class RemoveDupsStringArray {
public static void main(String[] args) {
String[] withDuplicates = new String[] {"one","one","two","three","one","three","three"};
String[] withoutDuplicates = new String[] {"one","two","three"};
removeDuplicates(withDuplicates);
removeDuplicates(withoutDuplicates);
}
private static void removeDuplicates(String[] array) {
int[] occurence = new int[array.length];
for (int i = 0; i < array.length; i++) {
for(int j=i+1;j<array.length;j++){
if(array[i]==array[j]){
occurence[j]=j;
}
}
}
int resultLength=0;
for(int i=0;i<occurence.length;i++){
if(occurence[i]==0){
resultLength++;
}
}
String[] result=new String[resultLength];
int index=0;int j=0;
for(int i=0;i<occurence.length;i++){
index = occurence[i];
if(index==0){
result[j]= array[i];
j++;
}
}
for(String eachString : result){
System.out.println(eachString);
}
}
}

ArrayList of ArrayLists - clear function confusion

Here is a particular method I have written:
class A {
private static ArrayList<ArrayList<Integer>> inputTerms = new ArrayList<ArrayList<Integer>();
public static void method1(ArrayList<Integer> terms) {
ArrayList<Integer> clauses = new ArrayList<Integer>();
int N = terms.size();
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
clauses.add(-terms.get(i));
clauses.add(-terms.get(j));
inputTerms.add(clauses);
clauses.clear();
}
}
}
}
This method is called multiple times from the main function.
In the end, i try to write the contents of the class variable into a file. However, when I do this, i get 0 as the contents of inputTerms. However, if i remove the clauses.clear() line, i am able to get approppriate values.
My program is such that it is vital for me to clear the clauses after adding to inputTerms. Is there any alternative to this?
**Hmmm.. I have done what you've suggested. However, I haven't quite overcome the problem. To give more background, in my main function, I have the following code:
for (int i=0; i<N-1; i++){
ArrayList<Integer> firstdiagonalTerms = new ArrayList<Integer>();
for (int j=0; j<N-i; j++){
firstdiagonalTerms.add(variable[j][i+j]);
}
method1(firstdiagonalTerms);
}
I have to call the method1 function 4 times for different combinations of 'i' and 'j'. However, I still get 0 when I use the above mentioned suggestions**
You are adding the same list and clearing it repeatedly. When you add an object to a list it copies a reference to it, not a copy of the object.
int N = terms.size();
for (int i = 0; i < N - 1; i++) {
for (int j = i + 1; j < N; j++) {
List<Integer> clauses = new ArrayList<Integer>();
clauses.add(-terms.get(i));
clauses.add(-terms.get(j));
inputTerms.add(clauses);
}
}
or
for (int i = 0, N = terms.size(); i < N - 1; i++)
for (int j = i + 1; j < N; j++)
inputTerms.add(Arrays.asList(-terms.get(i), -terms.get(j)));
Not sure i understand what you are trying to achieve, but you keep reusing the same list, which is probably not what you meant to do.
You should probably move the ArrayList<Integer> clauses = new ArrayList<Integer>(); inside the inner loop, and not call clauses.clear() at all.
When you are adding "clauses" you are adding the actual object to the arrayList, not a copy. So when you clear them all the values in the list will be removed. To get arround this, add a clone of the list:
inputTerms.add((ArrayList<Integer>) clauses.clone());
When you call clear() on list, you are updating/removing same objects (because list contains reference to objects, not copy of object). That is what causing the issue.
I think you need to do something like below. Instead of using clear(), create a new list everytime.
public static void method1 (ArrayList<Integer> terms)
{
int N = terms.size();
for (int i = 0; i<N-1; i++) {
for (int j=i+1; j<N; j++) {
ArrayList<Integer> clauses = new ArrayList<Integer>();
clauses.add(-terms.get(i));
clauses.add(-terms.get(j));
inputTerms.add(clauses);
}
}

Categories