Counting Occurences of int an array using List - java

So I'm trying to make a program that counts the occurences of int in an array. what i tried to do is to make a method that lists the unique integers, then another method to compare the list items to the original array items.
public List listUnique(int[] arr){
Arrays.sort(arr);
List <Integer> temp = new ArrayList<>();
int currentInt = 0;
for (int i = 0; i < arr.length; i++){
if(arr[i] != currentInt){
temp.add(arr[i]);
currentInt = arr[i];
}
}
return temp;
}
public int[] countDupli(List unique, int[] arr){
int [] ret = new int[unique.size()];
Iterator <Integer> iterator = unique.iterator();
for (int l = 0; l < unique.size(); l++){
ret[l] = iterator.next().intValue();
}
int[] dupli = new int[ret.length];
for (int j = 0; j < ret.length; j++){
dupli[j] = 0;
}
for (int k = 0; k < ret.length; k++){
for (int i = 0; i < arr.length; i++){
if ( ret[k] == arr[i]){
dupli[k]+= 1;
}
}
k++;
}
return dupli;
}
It isn't doing what is intended to do tho. For example, an input of {1,2,...,1,2} 10 items of those, prints the correct unique items but only outputs the count of 1 but not 2. dupli = [5,0]. where did the algorithm go wrong? thanks

In your code try debugging or just print ret[] values and see if all unique values are present.
Other Suggestions:
List <Integer> temp = new ArrayList<>();
int currentInt = 0;
for (int i = 0; i < arr.length; i++){
if(arr[i] != currentInt){
temp.add(arr[i]);
currentInt = arr[i];
}
You can simply use HashSet, then you don't have to write above code to find unique values. HashSet does not store duplicate values.
Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));
Then you can use iterator and compare with your sorted array and increase count simultaneously.
You can skip the following for loop, as java initializes it to 0 by default.
int[] dupli = new int[ret.length];
for (int j = 0; j < ret.length; j++){
dupli[j] = 0;
}

Related

keep array in Vector/ArrayList

Do I keep many arrays in a vector?
I want like this...
[{1,2,3},{4,5,6},{7,8,9},{10,11,12}]
I am trying in this way and get error.
Vector< int[]> v=new Vector();
for(int i=0;i<n;i++){
int a[]=new int[3];
for(int j=0;j<3;j++){
a[j]=ob.nextInt();
}
v.add(a);
}
for(int i=0;i<n;i++){
System.out.println(v.get(i));
}
I know this way
Vector< Vector<Integer>> v=new Vector();
for(int i=0;i<n;i++){
Vector< Integer> v1=new Vector();
for(int j=0;j<3;j++){
v1.add(ob.nextInt());
}
v.add(v1);
}
for(int i=0;i<n;i++){
System.out.println(v.get(i));
}
But I want to keep those element (in a vector) using array .Can I?
If you really want use Vector, you can try something like this (I assume, your ob is Random object):
Random ob = new Random();
final int n = 3;
Vector<int[]> v = new Vector<>();
for (int i = 0; i < n; i++) {
int[] a = new int[n];
for (int j = 0; j < n; j++) {
a[j] = ob.nextInt();
}
v.add(a);
}
for (int i = 0; i < v.size(); i++) { //changed
for (int j = 0; j < v.get(i).length; j++) {
System.out.println(v.get(i)[j]);
}
}
you were probably surprised by the display of int [] on your console. In my case:
[I#7229724f
[I#4c873330
[I#119d7047
You can read more about it here.
But I recommend change Vector to List (you can read this for more details).
You can also use NOT primitive type with
Vector<Integer[]> v = new Vector();
But, what's the problem? Where is the error?
Scanner ob = new Scanner(System.in).useDelimiter("\\s");
int n = 2;
Vector<int[]> v = new Vector();
for (int i = 0; i < n; i++) {
int[] a = new int[3];
for (int j = 0; j < 3; j++) {
a[j] = ob.nextInt();
}
v.add(a);
}
for (int i = 0; i < n; i++) {
System.out.println(Arrays.toString(v.get(i))); //change output
}
Input: 1 2 3 4 5 6
Output:
[1, 2, 3]
[4, 5, 6]

how to replace duplicate elements with a string in arrayList?

i want to replace duplicate element with "repeated" string for example,
the arraylist is ["a","b","b","c","a","c","a"], the new array is ["a","b","repeated","c","repeated","repeated","repeated]
i have tried this
ArrayList<String> m = new ArrayList();
m.add("a");
m.add("b");
m.add("b");
m.add("c");
m.add("a");
m.add("c");
m.add("a");
System.out.println(m);
for (int i = 0; i < m.size(); i++) {
for (int j = 0; j < m.size(); j++) {
if (m.get(i).equals(m.get(j))){
m.set(j, "repeated");
}
}
}
but it's not working
No need to two nested loop. just use a Set to store repeated string.
Set<String> repeat = new HashSet<>();
int index = -1;
for (String s:m){
index++;
m.set(index, repeat.add(s) ? s : "repeated");
}
Two errors:
You print your list before updating. Move the
System.println(...) after the loop.
You are replacing all elements (not just the duplicates) with the "repeated" String since
the first comparison in the loop is with the element
itself.
ArrayList<String> m = new ArrayList<>();
m.add("a");
m.add("b");
m.add("b");
m.add("c");
m.add("a");
m.add("c");
m.add("a");
for (int i = 0; i < m.size(); i++) {
for (int j = i + 1; j < m.size(); j++) {
if (m.get(i).equals(m.get(j))){
m.set(j, "repeated");
}
}
}
System.out.println(m);

JAVA - Compare two arrays and create a new array with only the unique values from the first

I have to solve an exercise with the following criteria:
Compare two arrays:
int[] a1 = {1, 3, 7, 8, 2, 7, 9, 11};
int[] a2 = {3, 8, 7, 5, 13, 5, 12};
Create a new array int[] with only unique values from the first array. Result should look like this: int[] result = {1,2,9,11};
NOTE: I am not allowed to use ArrayList or Arrays class to solve this task.
I'm working with the following code, but the logic for the population loop is incorrect because it throws an out of bounds exception.
public static int[] removeDups(int[] a1, int[] a2) {
//count the number of duplicate values found in the first array
int dups = 0;
for (int i = 0; i < a1.length; i++) {
for (int j = 0; j < a2.length; j++) {
if (a1[i] == a2[j]) {
dups++;
}
}
}
//to find the size of the new array subtract the counter from the length of the first array
int size = a1.length - dups;
//create the size of the new array
int[] result = new int[size];
//populate the new array with the unique values
for (int i = 0; i < a1.length; i++) {
int count = 0;
for (int j = 0; j < a2.length; j++) {
if (a1[i] != a2[j]) {
count++;
if (count < 2) {
result[i] = a1[i];
}
}
}
}
return result;
}
I would also love how to solve this with potentially one loop (learning purposes).
I offer following soulution.
Iterate over first array, and find out min and max it's value.
Create temporary array with length max-min+1 (you could use max + 1 as a length, but it could follow overhead when you have values e.g. starting from 100k).
Iterate over first array and mark existed values in temorary array.
Iterate over second array and unmark existed values in temporary array.
Place all marked values from temporary array into result array.
Code:
public static int[] getUnique(int[] one, int[] two) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < one.length; i++) {
min = one[i] < min ? one[i] : min;
max = one[i] > max ? one[i] : max;
}
int totalUnique = 0;
boolean[] tmp = new boolean[max - min + 1];
for (int i = 0; i < one.length; i++) {
int offs = one[i] - min;
totalUnique += tmp[offs] ? 0 : 1;
tmp[offs] = true;
}
for (int i = 0; i < two.length; i++) {
int offs = two[i] - min;
if (offs < 0 || offs >= tmp.length)
continue;
if (tmp[offs])
totalUnique--;
tmp[offs] = false;
}
int[] res = new int[totalUnique];
for (int i = 0, j = 0; i < tmp.length; i++)
if (tmp[i])
res[j++] = i + min;
return res;
}
For learning purposes, we won't be adding new tools.
Let's follow the same train of thought you had before and just correct the second part:
// populate the new array with the unique values
for (int i = 0; i < a1.length; i++) {
int count = 0;
for (int j = 0; j < a2.length; j++) {
if (a1[i] != a2[j]) {
count++;
if (count < 2) {
result[i] = a1[i];
}
}
}
}
To this:
//populate the new array with the unique values
int position = 0;
for (int i = 0; i < a1.length; i++) {
boolean unique = true;
for (int j = 0; j < a2.length; j++) {
if (a1[i] == a2[j]) {
unique = false;
break;
}
}
if (unique == true) {
result[position] = a1[i];
position++;
}
}
I am assuming the "count" that you implemented was in attempt to prevent false-positive added to your result array (which would go over). When a human determines whether or not an array contains dups, he doesn't do "count", he simply compares the first number with the second array by going down the list and then if he sees a dup (a1[i] == a2[j]), he would say "oh it's not unique" (unique = false) and then stop going through the loop (break). Then he will add the number to the second array (result[i] = a1[i]).
So to combine the two loops as much as possible:
// Create a temp Array to keep the data for the loop
int[] temp = new int[a1.length];
int position = 0;
for (int i = 0; i < a1.length; i++) {
boolean unique = true;
for (int j = 0; j < a2.length; j++) {
if (a1[i] == a2[j]) {
unique = false;
break;
}
}
if (unique == true) {
temp[position] = a1[i];
position++;
}
}
// This part merely copies the temp array of the previous size into the proper sized smaller array
int[] result = new int[position];
for (int k = 0; k < result.length; k++) {
result[k] = temp[k];
}
Making your code work
Your code works fine if you correct the second loop. Look at the modifications I did:
//populate the new array with the unique values
int counter = 0;
for (int i = 0; i < a1.length; i++) {
for (int j = 0; j < a2.length; j++) {
if (a1[i] == a2[j]) {
result[counter] = a1[i];
counter++;
}
}
}
The way I would do it
Now, here is how I would create a method like this without the need to check for the duplicates more than once. Look below:
public static int[] removeDups(int[] a1, int[] a2) {
int[] result = null;
int size = 0;
OUTERMOST: for(int e1: a1) {
for(int e2: a2) {
if(e1 == e2)
continue OUTERMOST;
}
int[] temp = new int[++size];
if(result != null) {
for(int i = 0; i < result.length; i++) {
temp[i] = result[i];
}
}
temp[temp.length - 1] = e1;
result = temp;
}
return result;
}
Instead of creating the result array with a fixed size, it creates a new array with the appropriate size everytime a new duplicate is found. Note that it returns null if a1 is equal a2.
You can make another method to see if an element is contained in a list :
public static boolean contains(int element, int array[]) {
for (int iterator : array) {
if (element == iterator) {
return true;
}
}
return false;
}
Your main method will iterate each element and check if it is contained in the second:
int[] uniqueElements = new int[a1.length];
int index = 0;
for (int it : a1) {
if (!contains(it, a2)) {
uniqueElements[index] = it;
index++;
}
}

Compressing array values into another array

I came across this problem called compress. The objective is to take an array, remove any repeated values and return a new array.
I know this would be very easy with ArrayLists, but I want to do it without them.
So far, I've just written a loop to determine the number of unique values so I can construct a new array of the appropriate length. How can I then get the unique values into the new array?
public static int[] compress(int[] array){
int length = 0;
boolean contains = false;
for (int i = 0; i < array.length; i++){
contains = false;
for (int j = 0; j < i; j++){
if (a[i] == a[j]){
contains = true;
j = i;
} else {
contains = false;
}
}
if (!contains){
length++;
}
}
int[] uniqueArray = new int[length];
}
Not Tested but I think this should do the trick.
public static int[] copyArray(int [] num){
int x = 0;
int numDuplicate = 0;
int[] copy = new int[num.length]; // we use this to copy the non duplicates
HashMap<Integer, Integer> count = new HashMap<>(); //hashmap to check duplicates
for(int i = 0; i < num.length; i++){
if(count.containsKey(num[i])){
count.put(num[i], count.get(num[i])+1);
numDuplicate++; // keep track of duplicates
}else{
count.put(num[i], 1); // first occurence
copy[x] = num[i]; // copy unique values, empty values will be at end
x++;
}
}
// return only what is needed
int newSize = num.length - numDuplicate;
int[] copyNum = new int[newSize];
for(int i = 0; i < copyNum.length; i++){
copyNum[i] = copy[i];
}
return copyNum;
}
public static void main(String[] args) {
// sample elements
int[] nums = new int[20];
for(int i = 0; i < nums.length; i++){
nums[i] = (int)(Math.random() * 20);
}
System.out.println(Arrays.toString(nums));
System.out.println(Arrays.toString(copyArray(nums)));
}

How do I remove duplicates from two arrays?

I need to have an algorithm that changes values in one array if it is in the second array. The result is that the first array should not have any values that are in the second array.
The arrays are of random length (on average ranging from 0 to 15 integers each), and the content of each array is a list of sorted numbers, ranging from 0 to 90.
public void clearDuplicates(int[] A, int[] B){
for(int i = 0; i < A.length; i++){
for(int j = 0; j < B.length; j++)
if(A[i] == B[j])
A[i]++;
}
}
My current code does not clear all of the duplicates. On top of that it might be possible it will creat an index out of bounds, or the content can get above 90.
Although your question is not very clear, this might do the job. Assumptions:
The number of integers in A and B is smaller than 90.
The array A is not sorted afterwards (use Arrays.sort() if you wish to
fix that).
The array A might contain duplicates within itself afterwards.
public void clearDuplicates(int[] A, int[] B) {
// Initialize a set of numbers which are not in B to all numbers 0--90
final Set<Integer> notInB = new HashSet<>();
for (int i = 0; i <= 90; i++) {
notInB.add(i);
}
// Create a set of numbers which are in B. Since lookups in hash set are
// O(1), this will be much more efficient than manually searching over B
// each time. At the same time, remove elements which are in B from the
// set of elements not in B.
final Set<Integer> bSet = new HashSet<>();
for (final int b : B) {
bSet.add(b);
notInB.remove(b);
}
// Search and remove duplicates
for (int i = 0; i < A.length; i++) {
if (bSet.contains(A[i])) {
// Try to replace the duplicate by a number not in B
if (!notInB.isEmpty()) {
A[i] = notInB.iterator().next();
// Remove the added value from notInB
notInB.remove(A[i]);
}
// If not possible, return - there is no way to remove the
// duplicates with the given constraints
else {
return;
}
}
}
}
You can do it just by using int[ ] although it's a bit cumbersome. The only constraint is that there may not be duplicates within B itself.
public void clearDuplicates(int[] A, int[] B) {
//Number of duplicates
int duplicate = 0;
//First you need to find the number of duplicates
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < B.length; j++)
if (A[i] == B[j])
duplicate++;
}
//New A without duplicates
int[] newA = new int[A.length-duplicate];
//For indexing elements in the new A
int notDuplicate = 0;
//For knowing if it is or isn't a duplicate
boolean check;
//Filling the new A (without duplicates)
for (int i = 0; i < A.length; i++) {
check = true;
for (int j = 0; j < B.length; j++) {
if (A[i] == B[j]) {
check = false;
notDuplicate--;//Adjusting the index
}
}
//Put this element in the new array
if(check)
newA[notDuplicate] = A[i];
notDuplicate++;//Adjusting the index
}
}
public class DuplicateRemove {
public static void main(String[] args) {
int[] A = { 1, 8, 3, 4, 5, 6 };
int[] B = { 1, 4 };
print(clear(A, B));
}
public static int[] clear(int[] A, int[] B) {
int a = 0;
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < B.length; j++) {
if (A[i] == B[j]) {
a++;
for (int k = i; k < A.length - a; k++) {
A[k] = A[k + 1];
}
}
}
}
int[] C = new int[A.length - a];
for (int p = 0; p < C.length; p++)
C[p] = A[p];
return C;
}
public static void print(int[] A) {
for (int i = 0; i < A.length; i++)
System.out.println("Element: " + A[i]);
}
}
Here is an example.. I compiled and its working. For any question just let me know :)
maybe you should try the following code:
public void clear (int[] A, int[] B)
{
for (int i=0; i<A.length;i++)
{
for (int j=0; j<B.length; j++)
if(A[i]==B[j])
{
for (int k=i; k<A.length;k++)
A[k]=A[k+1];
j=B.length-1; //so that the cycle for will not be executed
}
}
}

Categories

Resources