This question already has answers here:
How to count duplicate elements in ArrayList?
(13 answers)
Closed 4 years ago.
I must not modify the contents of the array, like using Array.sort().
Expected return of this code is 6, because there are three duplicates of the value 1, one duplicate of the value 2, and two duplicates of the value 4. but mine is showing 10. I know the reason why it is 10: it's counting the duplicates multiple times.
How can I make this code check duplicate integer only once?
private int count = 0;
public void run() {
int[] a = {1, 4, 2, 4, 7, 1, 1, 9, 2, 3, 4, 1};
println(countDuplicates(a));
}
private int countDuplicates(int[] a) {
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] == a[j]) {
count++;
}
}
}
return count;
}
One option would be to take the current size minus the size with duplicates removed:
Set<Integer> set = new HashSet<>();
for (int i=0; i < a.length; ++i) {
set.add(a[i]);
}
int numDuplicates = a.length - set.size();
There is probably a sleek way to populate a set of Integer from an array of primitive int using streams in Java 8.
Here you go with simple to understand logic for leaners
public static void main(String[] args) throws IOException {
int []a ={1, 4, 2, 4, 7, 1, 1, 9, 2, 3, 4, 1};
Map<Integer, Integer> occurances = new HashMap<>();
for(int i = 0; i < a.length; i++) {
//Check the is already occurred if not then add occurrence as 1
if (!occurances.containsKey(a[i])) {
occurances.put(a[i], 1);
}
// Second occurrences for a number
else {
occurances.put(a[i], occurances.get(a[i]) + 1);
}
}
System.out.println(occurances);
System.out.println("Total Numbers: "+a.length);
System.out.println("Duplicate count is "+ (a.length - occurances.size()));
}
Sort the array in-place.
Run through the array once, and count how many times current value is equal to previous value.
(Assuming you are allowed to use a library method to sort the array)
private static int countDuplicates(final int[] a)
{
int count = 0;
Arrays.sort(a); // sort array, you can use any way for it
for (int i = 0; i < a.length - 1; i++)
{
if (a[i] == a[i + 1]) // check only next element
{
count++;
}
}
return count; // return 6
}
Related
This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 12 months ago.
I'm just a beginner and I'm trying to write a method returning only unique elements from Java array. What's wrong with this code? Could you please point my mistakes and help me correct them? I've got no idea what to do with it.
P.S. I know I could use packages, but it has to be done in the simplest way.
public static void main(String[] args) {
int[] arr = {1, 1, 2, 3};
int[] items = returnNonRepeated(arr);
System.out.print(items);
}
public static int[] returnNonRepeated(int[] arr) {
int[] temp = new int[0];
int n = 0;
for (int i = 0; i < arr.length; i++) {
if (i == 0) {
temp[i] = arr[i];
} else {
if (arr[i] != arr[i - 1]) {
temp[numbersCount] = arr[i];
numbersCount++;
}
}
}
return temp;
}
}
I know it's not code golf here, but:
if you want to keep only unique values (like a Set would do), you can use a "one-liner". You don't have to implement loops.
public static void main(String[] args) {
int[] dupes = {0, 1, 2, 1, 3, 2, 3};
int[] uniques = Arrays.stream(dupes) // stream ints
.boxed() // make Integer of int
.collect(Collectors.toSet()) // collect into set to remove duplicates
.stream() // stream Integers
.mapToInt(Integer::intValue) // make int of Integer
.toArray(); // and back into an array
System.out.println(Arrays.toString(uniques));
}
Please not that you have to use java.util.Arrays.toString() to get useful output in the last statement. With your variant it will print only something like "[I#531be3c5" which denotes that it is an array of int, but says nothing about the contents.
Nevertheless - if you want to understand, how arrays and loops work, the looping solution is better!
I'm not sure if you mean non-repeat or unique
But here is a method for checking for duplicates:
public static int[] returnUniqueArray(int[] arr) {
int dupes = new int[arr.length]; // The most dupes you could have is the length of the input
int numberOfDupes = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if(arr[i] == arr[j]) {
dupes[numberOfDupes] = arr[i];
numberOfDupes++;
}
}
}
return dupes;
}
This loops round the arr array twice, therefore comparing every variable in arr to every other variable. If they are the same, it adds that value to a new array.
You could also do similar for non-repeat:
public static int[] returnNonRepeatArray(int[] arr) {
int dupes = new int[arr.length];
int numberOfDupes = 0;
for (int i = 0; i < arr.length - 1; i++) {
if(arr[i] == arr[i+1]) {
dupes[numberOfDupes] = arr[i];
numberOfDupes++;
}
}
return dupes;
}
This loops round the arr array once, but does not check the last item (-1 in the for loop). It then compares the item against the next item, if they are the same it adds that to the dupes.
public static void main(String[] args) {
int[] arr = {1, 1, 2, 2, 3, 4, 5, 5, 5, 6};
int[] items = returnNonRepeated(arr);
System.out.print(Arrays.toString(items));
}
public static int[] returnNonRepeated(int[] arr) {
int[] temp = new int[arr.length];
int numbersCount = 1;
for (int i = 0; i < arr.length; i++) {
if (i == 0) {
uniques[numbersCount] = arr[i];
numbersCount++;
} else {
if (arr[i] != arr[i - 1]) {
uniques[numbersCount] = arr[i];
numbersCount++;
}
}
}
return uniques;
}
Given a sorted array A of size N, delete all the duplicates elements from A.
Note: Don't use set or HashMap to solve the problem.
example:
Input:
N = 5
Array = {2, 2, 2, 2, 2}
Output:
2
Explanation: After removing all the duplicates
only one instance of 2 will remain.
I have tried the below code. Please tell me what's wrong with the code?
int remove_duplicate(int arr[],int N){
// code here
int index=0;
for(int i=1;i<N;i++){
if(arr[i]!=arr[i-1]){
arr[index]=arr[i-1];
index++;
}
}
return index+1;
}
You could replace some duplicate elements and set the length at the end.
This approach mutates the array.
const
remove_duplicate = array => {
let j = 0;
for (let i = 0; i < array.length; i++) {
if (array[j - 1] !== array[i]) array[j++] = array[i];
}
array.length = j;
return array;
};
console.log(...remove_duplicate([2, 2, 2]));
console.log(...remove_duplicate([1, 1, 2, 2, 3, 4, 5, 5]));
Overkill using binary tree
BinaryTree binaryTree = new BinaryTree();
for (int i = 0; i < n; i++){
//add or insert function need to check that the key isn't in the stracture
binaryTree.Add(arr[i]);
}
binaryTree.TraverseInOrder(binaryTree.Root);
You need to implement some of the classes.
Check this example:
c-binary-search-tree-implementation
For more on Inorder output for binaryTree:
binary-tree-from-inorder-traversal
Look here: https://www.geeksforgeeks.org/duplicates-array-using-o1-extra-space-set-2/
// Java program to print all elements that
// appear more than once.
import java.util.*;
class GFG {
// function to find repeating elements
static void printRepeating(int arr[], int n)
{
// First check all the values that are
// present in an array then go to that
// values as indexes and increment by
// the size of array
for (int i = 0; i < n; i++)
{
int index = arr[i] % n;
arr[index] += n;
}
// Now check which value exists more
// than once by dividing with the size
// of array
for (int i = 0; i < n; i++)
{
if ((arr[i] / n) >= 2)
System.out.println(i + " ");
}
}
// Driver code
public static void main(String args[])
{
int arr[] = { 1, 6, 3, 1, 3, 6, 6 };
int arr_size = arr.length;
System.out.println("The repeating elements are: ");
// Function call
printRepeating(arr, arr_size);
}
}
Try this:
int remove_duplicate(int arr[],int N){
int index=0;
for(int i=1;i<N;i++){
if(arr[i]!=arr[index]){ //change index
index++; //swapt next line
arr[index]=arr[i];
}
}
return index+1;
}
you can check my answer here - and it works perfectly
https://stackoverflow.com/a/32931932/3052125
Here is the main logic to remove the duplicates - arr is the sorted array provided to you with duplicate elements -
// Logic for removing the duplicate elements
int compare = 0;
arr[compare] = arr[0];
for (int i = 1; i < size; i++) {
if (arr[compare] != arr[i]) {
compare++;
arr[compare] = arr[i];
}
}
Given a sorted array A of size N, delete all the duplicates elements from A
Well, that implies returning an array with duplicates deleted.
int[] v = { 1, 1, 1, 2, 2, 2, 2,3 };
v = remove_duplicates(v);
System.out.println(Arrays.toString(v));
prints
[1, 2, 3]
The method. This works by copying the unique values toward the front of the passed array. So this does alter that array.
public static int[] remove_duplicates(int[] v) {
int current = 0;
for (int i = 0; i < v.length; i++) {
if (v[i] != v[current]) {
v[++current] = v[i];
}
}
// Now return the front portion of the array that contains the distinct
// values. You could also just create a new array and copy the elements
// using a loop.
return Arrays.copyOf(v, current+1);
}
This question already has answers here:
Remove all zeros from array
(11 answers)
Closed 4 years ago.
Can you please advise how can I pass results from an array to another array without some numbers? In this case without zero numbers. Here is my code, I'm stuck.
Result is: [0, 1, 2, 3, 0, 5, 0, 7, 0, 0, 0, 11] - all I want to do, is to create another array and pass there numbers from above without 0's.
Thanks.
public class SieveOfEratosthenes {
public static void main(String[] args) {
int[] myArray = new int[12];
fillArrayWithNumbers(myArray);
System.out.println(Arrays.toString(sieve(myArray)));
}
private static void fillArrayWithNumbers(int[] myArray) {
for (int i = 0; i < myArray.length; i++) {
myArray[i] = i;
}
}
public static int[] sieve(int[] maximumNumber) {
for (int j = 2; j < maximumNumber.length; j++) {
for (int i = j * 2; i < maximumNumber.length; i += j) {
maximumNumber[i] = 0;
}
}
return maximumNumber;
}
}
You can convert your Int[] to a Stream and filter out the zero values.
Code
public static void main(String[] args) {
int[] arr = new int[]{0, 1, 2, 3, 0, 5, 0, 7, 0, 0, 0, 11};
int[] ints = Arrays.stream(arr)
.filter(i -> i != 0)
.toArray();
Arrays.stream(ints).forEach(System.out::println);
}
Output
1
2
3
5
7
11
The solution is above as what infinitezero said to write a for loop that has you array length. Then copy items in the array as length.
for(int i = 0; i < arr.length; i++){
if(arr[i] == 0){
}
else{
arr[i] = arr2[i];
}
I think you can create a new array and fill it with above zero value:
What you want to do it to omit all even numbers (except 2). Then it should be done like this:
public static int[] sieve(int[] maximumNumber) {
int[] result = new int[maximumNumber.length];
int index= 0;
for (int i = 1; i < maximumNumber.length; i++) {
if (i == 2 || i % 2 != 0) { // i is 2 or odd
result[index] = i;
index++; // This will store the real length of the array
}
}
return Arrays.copyOfRange(result, 0, index);
}
This question already has answers here:
How to efficiently remove duplicates from an array without using Set
(48 answers)
Closed 7 years ago.
I was asked to write a method that accepts a sorted array, removes any duplicate elements found in the array and then places a 0 at the end of the array for every duplicate element found.
It is also supposed to return the number of unique elements found in the array.
Here is my method:
public static int removeDups(int[] arr) {
int j = 0;
int i = 1;
int numDups = 0;
while(i < arr.length) {
if (arr[i] == arr[j]) {
i++;
numDups++;
}
else {
arr[++j] = arr[i++];
}
}
for (int k = j+1; k < arr.length; k++) {
arr[k] = 0;
}
return (j);
}
It successfully finds all the duplicate numbers in the array and places the correct number of 0s at the end, but it doesn't always return the correct value for the number of unique elements.
For example, for the array:
{ 6 10 19 21 23 26 27 36 38 45 }
the number of unique elements should be 10, but it returns 9.
What am I doing wrong?
As it can be seen, j is used as the index of the last unique element.
In an array, i'th index is actually the i + 1'th element counted from 1.
So, you have to return j + 1 instead of j from your method.
Here is a solution to your problem. It keeps track of two pointers, one which only advances when a value gets written to the array, and the other which touches every element of the array in sequential order. When one or more duplicates are encountered, the second pointer keeps advancing, while the first pointer stays put, waiting to write a non-duplicate value. Finally, the code iterates over the remainder of the array from the first pointer, writing out zeroes until the end.
public static int removeDups(int[] arr) {
if (arr == null) {
return null;
}
if (arr.length == 0 || arr.length == 1) {
return arr;
}
int prevIndex = 0;
for (int i=1; i < arr.length; ++i) {
if (arr[prevIndex] != arr[i]) {
arr[prevIndex+1] = arr[i];
++prevIndex;
}
}
for (int i=prevIndex+1; i < arr.length; ++i) {
arr[i] = 0;
}
return prevIndex+1;
}
int[] arr = {1, 2, 3, 3, 4, 5, 6, 6, 6, 10};
removeDups(arr);
System.out.println(Arrays.toString(arr));
Output:
[1, 2, 3, 4, 5, 6, 10, 0, 0, 0]
This code has been tested using IntelliJ and it appears to be working.
Try this!
static int getUniqueElements(int [] sortedArr){
int duplicateCount = 0;
int [] tempArr = sortedArr;
int j=0;
boolean isNewValue = true;
for(int i=1;i<tempArr.length;i++){
if(sortedArr[j] != tempArr[i]){
isNewValue = true;
sortedArr[++j] = tempArr[i];
}else{
if(isNewValue){
isNewValue = false;
duplicateCount++;
}
}
}
for(j++;j<sortedArr.length;j++){
sortedArr[j] = 0;
duplicateCount++;
}
return (sortedArr.length-duplicateCount);
}
public static void main(String[] args) {
int[] arr = {1, 3, 3, 3, 3, 6, 6, 7, 8, 8};
System.out.println("Unique Count:"+ getUniqueElements(arr));
System.out.println(Arrays.toString(arr));
}
OutPut:
Unique Count:2
[1, 3, 6, 7, 8, 0, 0, 0, 0, 0]
Since in the given array 1,7 are unique.
Note: tried with your example array {6, 10, 19, 21, 23 ,26 ,27 ,36 ,38, 45 } also
My solution is as(assuming elements can repeat only twice):
public static int removeDups(int[] arr) {
int i = 0;
int numDups = 0;
while (i < arr.length - 1 - numDups) {
if (arr[i] == arr[i + 1]) {
numDups++;
for (int m = i + 1; m < arr.length - numDups; m++) {
arr[m] = arr[m + 1];
}
arr[arr.length - numDups] = 0;
}
i++;
}
return arr.length-numDups;
}
I am trying to remove duplicates from a list by creating a temporary array that stores the indices of where the duplicates are, and then copies off the original array into another temporary array while comparing the indices to the indices I have stored in my first temporary array.
public void removeDuplicates()
{
double tempa [] = new double [items.length];
int counter = 0;
for ( int i = 0; i< numItems ; i++)
{
for(int j = i + 1; j < numItems; j++)
{
if(items[i] ==items[j])
{
tempa[counter] = j;
counter++;
}
}
}
double tempb [] = new double [ items.length];
int counter2 = 0;
int j =0;
for(int i = 0; i < numItems; i++)
{
if(i != tempa[j])
{
tempb[counter2] = items[i];
counter2++;
}
else
{
j++;
}
}
items = tempb;
numItems = counter2;
}
and while the logic seems right, my compiler is giving me an arrayindexoutofbounds error at
tempa[counter] = j;
I don't understand how counter could grow to above the value of items.length, where is the logic flaw?
You are making things quite difficult for yourself. Let Java do the heavy lifting for you. For example LinkedHashSet gives you uniqueness and retains insertion order. It will also be more efficient than comparing every value with every other value.
double [] input = {1,2,3,3,4,4};
Set<Double> tmp = new LinkedHashSet<Double>();
for (Double each : input) {
tmp.add(each);
}
double [] output = new double[tmp.size()];
int i = 0;
for (Double each : tmp) {
output[i++] = each;
}
System.out.println(Arrays.toString(output));
Done for int arrays, but easily coud be converted to double.
1) If you do not care about initial array elements order:
private static int[] withoutDuplicates(int[] a) {
Arrays.sort(a);
int hi = a.length - 1;
int[] result = new int[a.length];
int j = 0;
for (int i = 0; i < hi; i++) {
if (a[i] == a[i+1]) {
continue;
}
result[j] = a[i];
j++;
}
result[j++] = a[hi];
return Arrays.copyOf(result, j);
}
2) if you care about initial array elements order:
private static int[] withoutDuplicates2(int[] a) {
HashSet<Integer> keys = new HashSet<Integer>();
int[] result = new int[a.length];
int j = 0;
for (int i = 0 ; i < a.length; i++) {
if (keys.add(a[i])) {
result[j] = a[i];
j++;
}
}
return Arrays.copyOf(result, j);
}
3) If you do not care about initial array elements order:
private static Object[] withoutDuplicates3(int[] a) {
HashSet<Integer> keys = new HashSet<Integer>();
for (int value : a) {
keys.add(value);
}
return keys.toArray();
}
Imagine this was your input data:
Index: 0, 1, 2, 3, 4, 5, 6, 7, 8
Value: 1, 2, 3, 3, 3, 3, 3, 3, 3
Then according to your algorithm, tempa would need to be:
Index: 0, 1, 2, 3, 4, 5, 6, 7, 8, ....Exception!!!
Value: 3, 4, 5, 6, 7, 8, 4, 5, 6, 7, 8, 5, 6, 7, 8, 6, 7, 8, 7, 8, 8
Why do you have this problem? Because the first set of nested for loops does nothing to prevent you from trying to insert duplicates of the duplicate array indices!
What is the best solution?
Use a Set!
Sets guarantee that there are no duplicate entries in them. If you create a new Set and then add all of your array items to it, the Set will prune the duplicates. Then it is just a matter of going back from the Set to an array.
Alternatively, here is a very C-way of doing the same thing:
//duplicates will be a truth table indicating which indices are duplicates.
//initially all values are set to false
boolean duplicates[] = new boolean[items.length];
for ( int i = 0; i< numItems ; i++) {
if (!duplicates[i]) { //if i is not a known duplicate
for(int j = i + 1; j < numItems; j++) {
if(items[i] ==items[j]) {
duplicates[j] = true; //mark j as a known duplicate
}
}
}
}
I leave it to you to figure out how to finish.
import java.util.HashSet;
import sun.security.util.Length;
public class arrayduplication {
public static void main(String[] args) {
int arr[]={1,5,1,2,5,2,10};
TreeSet< Integer>set=new TreeSet<Integer>();
for(int i=0;i<arr.length;i++){
set.add(Integer.valueOf(arr[i]));
}
System.out.println(set);
}
}
You have already used num_items to bound your loop. Use that variable to set your array size for tempa also.
double tempa [] = new double [num_items];
Instead of doing it in array, you can simply use java.util.Set.
Here an example:
public static void main(String[] args)
{
Double[] values = new Double[]{ 1.0, 2.0, 2.0, 2.0, 3.0, 10.0, 10.0 };
Set<Double> singleValues = new HashSet<Double>();
for (Double value : values)
{
singleValues.add(value);
}
System.out.println("singleValues: "+singleValues);
// now convert it into double array
Double[] dValues = singleValues.toArray(new Double[]{});
}
Here's another alternative without the use of sets, only primitive types:
public static double [] removeDuplicates(double arr[]) {
double [] tempa = new double[arr.length];
int uniqueCount = 0;
for (int i=0;i<arr.length;i++) {
boolean unique = true;
for (int j=0;j<uniqueCount && unique;j++) {
if (arr[i] == tempa[j]) {
unique = false;
}
}
if (unique) {
tempa[uniqueCount++] = arr[i];
}
}
return Arrays.copyOf(tempa, uniqueCount);
}
It does require a temporary array of double objects on the way towards getting your actual result.
You can use a set for removing multiples.