I have got two arraylists :
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(2);
numbers.add(0);
numbers.add(0);
numbers.add(8);
ArrayList<String> linkers = new ArrayList<>();
linkers.add("five");
linkers.add("two");
linkers.add("zero");
linkers.add("zero");
linkers.add("eight");
I need to sort the numbers list in ascending order and get the linkers list sorted in the same order.
Assuming there is a one-to-one mapping of the number to their name you can do it like so. Just sort the indices based on the list of numeric numbers. Then use those indices to get each list's values in the proper, sorted order. Here, I just print them to show the results.
List<Integer> indices = IntStream.range(0, numbers.size()).boxed()
.sorted(Comparator.comparing(numbers::get)).toList();
for (int i : indices) {
System.out.println(numbers.get(i) + " " + linkers.get(i));
}
prints
0 zero
0 zero
2 two
5 five
8 eight
They could be "sorted" as follows:
numbers = indices.stream().map(numbers::get).toList();
linkers = indices.stream().map(linkers::get).toList();
System.out.println(numbers);
System.out.println(linkers);
prints
[0, 0, 2, 5, 8]
[zero, zero, two, five, eight]
Parallel lists/arrays are trouble. Put corresponding elements into combined objects, then sort those.
import java.util.ArrayList;
import java.util.Comparator;
class Pair {
public int i;
public String s;
public Pair(int _i, String _s) {
i = _i;
s = _s;
}
}
class Test {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(2);
numbers.add(0);
numbers.add(0);
numbers.add(8);
ArrayList<String> linkers = new ArrayList<>();
linkers.add("five");
linkers.add("two");
linkers.add("zero");
linkers.add("zero");
linkers.add("eight");
ArrayList<Pair> pairs = new ArrayList<>();
for (int i = 0; i < 5; i++) {
pairs.add(new Pair(numbers.get(i), linkers.get(i)));
}
pairs.sort(new Comparator<Pair>() {
public int compare(Pair a, Pair b) {
if (a.i == b.i) return 0;
else if (a.i < b.i) return -1;
else return 1;
}
});
for (int i = 0; i < 5; i++) {
System.out.println(pairs.get(i).s);
}
}
}
One possibility would be to group each number (int) with its name (String) in a class (Java < 15) or record (Java >= 15):
record NumberWithName(int value, String name) {
}
Then, for each pair of int and String from the two Lists, construct a Number-instance and add it to a new List numbersWithName:
List<Integer> values = List.of(5, 2, 0, 0, 8);
List<String> names = List.of("five", "two", "zero", "zero", "eight");
List<NumberWithName> numbersWithName = new ArrayList<>();
for (int index = 0; index < values.size(); ++index) {
numbersWithName.add(new NumberWithName(values.get(index), names.get(index)));
}
Finally, we sort this List with a corresponding Comparator and print the result:
numbersWithName.sort(Comparator.comparing(NumberWithName::value));
System.out.println(numbersWithName);
This produces the following output:
[NumberWithName[value=0, name=zero], NumberWithName[value=0, name=zero], NumberWithName[value=2, name=two], NumberWithName[value=5, name=five], NumberWithName[value=8, name=eight]]
A TreeMap should help here where your key is the integer and the value represents the string. TreeMap is directly sorted by the key.
Related
Here is my problem:
I have a list of integers: 7,0,2
If I sort the list using Collections.sort(list) the result is: 0,2,7
but I want to sort the list excluding the 0 from the sorting procedure so the output looks like this: 2,0,7.
Is it possible?
Thanks in advance.
EDIT: I've forgot to mention my 3 possible cases:
1) list contains only one "0" and two numbers
2) list contains two "0" and one number
3) list contains three "0" and no numbers
You can do it but not only with Collections.sort()
Retrieve and store in a variable the index where the 0 Integer is :
Remove the 0 from the List with List.remove(int) where int is the index.
Sort the list with Collections.sort()
Add 0 in the List at the stored index.
In code, it gives :
List<Integer> list = ...;
int indexOf = list.indexOf(Integer.valueOf(0));
list.remove(indexOf);
Collections.sort(list);
list.add(indexOf, Integer.valueOf(0));
Update after question edit to handle cases with more than one 0 in the List.
I updated because this case is a little more complex to handle.
As it removes more than one element, the index is not any longer the index of the original size.
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(7);
list.add(0);
list.add(2);
list.add(9);
list.add(0);
list.add(1);
list.add(0);
list.add(4);
Set<Integer> indexesOf = new HashSet<>();
int indexOf = -1;
int shift = 0;
while ((indexOf = list.indexOf(Integer.valueOf(0))) != -1) {
indexesOf.add(indexOf + shift++);
list.remove(indexOf);
}
Collections.sort(list);
indexesOf.stream().forEach(index -> list.add(index, Integer.valueOf(0)));
System.out.println(list);
}
Output :
[1, 0, 2, 4, 0, 7, 0, 9]
Bubble sort it's your friend!
public static void main(String[] args) {
List<Integer> list = Arrays.asList(7, 0, 2);
for (int i = 0; i < list.size() - 1; i++) {
int a = list.get(i);
for (int j = i + 1; a != 0 && j < list.size(); j++) {
int b = list.get(j);
if (b != 0 && b < a){
list.set(i, b);
list.set(j, a);
a = b; // EDITED
}
}
}
System.out.println(list);
}
I have this code which randomly generates arrays of different lenghts (but with specific items according to my needs).
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
public class MarketBasketGeneratorAnon {
public static void main(String[] args) {
String[] elements = new String[]{"1", "2", "3", "4", "5", "6", "7","8"};
for (int i = 0; i < 99; i++) {
final String[] array = generateRandomArrayFromElements(elements);
Arrays.sort(array);
System.out.println(Arrays.toString(array));
}
}
private static String[] generateRandomArrayFromElements(String[] elements) {
int size = ThreadLocalRandom.current().nextInt(1, elements.length) + 1;
String[] array = new String[size];
ArrayList<Integer> usedIndices = new ArrayList<>(size);
for (int i = 0; i < array.length; i++) {
int randomIndex = getUniqueRandomIndex(usedIndices, size);
usedIndices.add(randomIndex);
array[i] = elements[randomIndex];
}
return array;
}
private static int getUniqueRandomIndex(ArrayList<Integer> usedIndices, int max) {
int randomIndex = ThreadLocalRandom.current().nextInt(0, max);
final boolean contains = usedIndices.contains(randomIndex);
if (contains)
randomIndex = getUniqueRandomIndex(usedIndices, max);
return randomIndex;
}
}
In this case, as you can see, I'm generating 100 arrays.
Then, I sort the elements in each array.
After sorting, of course many of these arrays become identical, having same lenght and same items in order.
I would like to print just one from each group of identical arrays, so that I get only unique arrays (no array is duplicated).
How can I edit my code to achieve that?
Thanks!
You can add your arrays to a Set: these are containers which don't permit duplicates.
Actually, you can't add arrays directly, because arrays don't implement the necessary methods such that two arrays with the same contents are considered equal (equals and hashCode); but you can wrap them in a data structure which does.
Set<List<String>> seenAlready = new HashSet<>();
// ...
for (int i = 0; i < 99; i++) {
final String[] array = generateRandomArrayFromElements(elements);
Arrays.sort(array);
if (seenAlready.add(Arrays.asList(array)) {
System.out.println(Arrays.toString(array));
}
}
Set.add returns true if the parameter is not already in the set. As such, this will print the array only if it wasn't already in the seenAlready set; and it adds the list representation of the array to the set so it won't be printed again.
I'm trying to count how many duplicate items are in an array.
Example:
[0, 2, 0] would return 2, [0, 0, 0] would return 3, [0, 1, 2] = 0
So far I have it working for when all three items are equal, but I'm not sure why it's returning one less than what it should for 2 items being the same.
int equal = 0;
for(int i = 0; i < recent.length; i++) {
for(int j = i; j < recent.length; j++) {
if(i != j && recent[i].equals(recent[j])) {
equal++;
}
}
}
Your algorithm is flawed in the following way: for every element in the array you look at all the elements after that element and if they happen to be equal, you increase the counter. However when you have 3 same elements, you count the last one twice - when you run internal loop for first and for second element. Moreover you never count the first element.
So it works by accident for [0, 0, 0] but doesn't work for other inputs.
I think that having nested loops is quite inefficient. You should be able to do it in o(n) rather than o(n^2).
If you time yours against the following...
public void run() {
int[] array = createRandomArray(2000000, 1000000);
System.out.println(countNumDups1(array));
}
private int[] createRandomArray(int numElements, int maxNumExclusive) {
int[] array = new int[numElements];
Random random = new Random();
for (int i = 0; i < array.length; i++) {
array[i] = random.nextInt(maxNumExclusive);
}
return array;
}
private int countNumDups1(int[] array) {
Map<Integer, Integer> numToCountMap = new HashMap<>();
for (int i = 0; i < array.length; i++) {
Integer key = array[i];
if (numToCountMap.containsKey(key)) {
numToCountMap.put(key, numToCountMap.get(key) + 1);
}
else {
numToCountMap.put(key, 1);
}
}
int numDups = 0;
for (int i = 0; i < array.length; i++) {
Integer key = array[i];
if (numToCountMap.get(key) > 1) {
numDups++;
}
}
return numDups;
}
I think you'll find the above is much faster even considering the horrible inefficiency of autoboxing and object creation.
The code you gave counts equivalences, so it adds one every time an element equals another element.
It sounds like what you want is the number of duplicate items, which is the same as (length - number of items that don't have a duplicate). I will call the latter "uniqueItems".
I would recommend the following:
// set of every item seen
Set<Integer> allItems = new HashSet<Integer>();
// set of items that don't have a duplicate
Set<Integer> uniqueItems = new HashSet<Integer>();
for(int i = 0; i < recent.length; i++) {
Integer val = i;
if(allItems.contains(val)) {
// if we've seen the value before, it is not a "uniqueItem"
uniqueItems.remove(val);
} else {
// assume the value is a "uniqueItem" until we see it again
uniqueItems.add(val);
}
allItems.add(val);
}
return recent.length - uniqueItems.size();
The below code works perfectly to find the duplicates
int array[] = {1,2,3,4,5,2,3,4,5,3,4,5,4,5,5};
HashMap<Integer,Integer> duplicates = new HashMap<Integer,Integer>();
for(int i=0; i<array.length; i++)
{
if(duplicates.containsKey(array[i]))
{
int numberOfOccurances = duplicates.get(array[i]);
duplicates.put(array[i], (numberOfOccurances + 1));
}else{
duplicates.put(array[i], 1);
}
}
Iterator<Integer> keys = duplicates.keySet().iterator();
System.out.print("Duplicates : " );
while(keys.hasNext())
{
int k = keys.next();
if(duplicates.get(k) > 1)
{
System.out.print(" "+k);
}
}
You are counting the number of pairs of indices that have equal values. What you claim to want is the total size of all sets of equal elements that have more than one element in them.
I would use a Map or similar to count the total number of appearances of a given value. At the end, iterate over the key values adding the number of appearances for each key that has more than one appearance.
int intArray[] = {5, 1, 2, 3, 4, 5, 3, 2};
String val = "";
int c = 1;
Map<Integer, Integer> nwmap = new HashMap<Integer, Integer>();
for (int i = 0; i < intArray.length; i++) {
Integer key = intArray[i];
if(nwmap.get(key) != null && nwmap.containsKey(key)){
val += " Duplicate: " +String.valueOf(key)+"\n";
}else{
nwmap.put(key, c);
c++;
}
}
LOG.debug("duplicate value:::"+val);
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class ArrayDuplicateCount {
/**
* #author:raviteja katari
*/
public static void main(String[] args) {
int intArray[] = {5, 1,4,4,4,5,1,2,1,2,5,5};
//for counting duplicate items
int c = 0;
//creating map collection to hold integers as keys and Cont as value
Map<Integer, Integer> nwmap = new LinkedHashMap<Integer, Integer>();
for (int i = 0; i <intArray.length; i++) {
//Assigning array element to key
Integer key = intArray[i];
//this code checks for elemnt if present updates count value else
//put the new Array elemnt into map and increment count
if(nwmap.containsKey(key)){
//updating key value by 1
nwmap.put(key, nwmap.get(key) + 1);
}else{
//Adding new array element to map and increasing count by 1
nwmap.put(key, c+1);
}
}
//printing map
System.out.println(nwmap);
}
}
output:
{5=4, 1=3, 4=3, 2=2}
public void TotalduplicateNumbers {
int a[] = {2,8,2,4,4,6,7,6,8,4,5};
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
for(int i=0;i<a.length;i++){
if(!m.containsKey(a[i]))
{
m.put(a[i], 1);
}
else
{
m.put(a[i], (m.get(a[i])+1));
}
}
for(Integer i:m.keySet()){
System.out.println("Number "+i+" "+"Occours "+m.get(i)+" time,");
}
}
We have an array containing 11 numbers, The logic is to create a map using these no. in which KEYS of map would be the actual number that must be entered by user and no. of occournce of that actual no. would be the value of that KEY. Here, containsKey() method checks whether the map contain that key already and return boolean value true or false as applied.If it does not contain then add that key into the map and its corresponding value should be 1 otherwise key would have already be contained in map so get the value of that key using get() and increment it by 1. Finally printing the map.
OUTPUT:--
Number 2 Occours 2 time,
Number 4 Occours 3 time,
Number 5 Occours 1 time,
Number 6 Occours 2 time,
Number 7 Occours 1 time,
Number 8 Occours 2 time,
I know most people don't like writing methods for people but i was hoping someone could help me convert my algorithm into Java code. I hope my algorithm is good and actually works.
Sort a given array of ints into ascending order. Set Group Limit to 15 (that means that the sum of the group is not greater than 15).
Take the first element of the sorted array and insert into a Group (new array/list) eg. Group A.
Take the second element of the sorted array and insert unless it will make it exceed the group limit. If it exceeds, create a new Group B and insert there.
Take third element and try to insert into next available group.
Repeat until all ints have been checked and grouped.
Input:
egArray = [1,3,4,6,6,9,12,14]
Output:
Group A: [1,3,4,6], Group B: [6,9], Group C: [12], Group D: [14]
I have tried to do this, but failed epically, not even worth me posting my code. :-(
This is an example data and an algorithm I've made up for self learning, so please keep the criticism to a minimum. I genuinely learn from a lot of Stackoverflow posts people have written over the last few months, unfortunately I couldn't find one like this example. Thanks.
Try this:
public static void main(String[] arguments) {
int limit = 15;
int[] egArray = new int[] { 14, 1, 3, 4, 6, 6, 9, 12 };
ArrayList<ArrayList<Integer>> a = grouping(limit, egArray);
System.out.println(a);
}
public static ArrayList<ArrayList<Integer>> grouping(int limit, int[] array) {
// Sort the input array.
Arrays.sort(array);
// Copy the int[] to an ArrayList<Integer>
ArrayList<Integer> input = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
input.add(array[i]);
}
// Initialize the groups
ArrayList<ArrayList<Integer>> groups = new ArrayList<>();
groups.add(new ArrayList<Integer>());
// Initialize the sums of the groups, to increase performance (I guess).
ArrayList<Integer> sums = new ArrayList<>();
sums.add(0);
// Iterate through the input array until there is no number
// left in it (that means we just added all the numbers
// into our groups array).
while (!input.isEmpty()) {
int n = input.get(0); // Store the number to 'n', to shortcut.
if (n > limit) {
String msg = "number is greater than the limit; cannot add number";
throw new IllegalArgumentException(msg);
// Or whatever to do if the number is larger than the limit.
}
boolean match = false;
// Search the next groups and check if our current
// number ('n') fits.
for (int i = 0; i < sums.size(); i++) {
if (sums.get(i) + n <= limit) {
// If it fits, then add the number to the group.
sums.set(i, sums.get(i) + n);
groups.get(i).add(n);
match = true;
break;
}
}
// If 'n' doesn't fit in any group, create a new one.
if (!match) {
ArrayList<Integer> e = new ArrayList<>();
e.add(n);
groups.add(e);
sums.add(n);
}
// Remove our number.
input.remove(0);
}
return groups;
}
Notice that the method returns an ArrayList<ArrayList<Integer>> instead of an int[][], but the effect is the same. In order to check the values of the groups, just run the main(String).
How about this method?
public static ArrayList group(ArrayList<Integer> arr, Integer groupLimit) {
ArrayList<ArrayList> result = new ArrayList<ArrayList>();
ArrayList<Integer> temp = new ArrayList<Integer>();
for (Integer x : arr) {
if (sumElements(temp) + x < groupLimit) {
temp.add(x);
} else {
result.add(temp);
temp = new ArrayList<Integer>();
temp.add(x);
}
}
if (temp.size() > 0) {
result.add(temp);
}
return result;
}
public static int sumElements(ArrayList<Integer> arr) {
Integer result = 0;
for(Integer x:arr) result += x;
return result;
}
Suppose the user enter an array, for example:
Array = {France, Spain, France, France, Italy, Spain, Spain, Italy}
which I did know the length of it
the index array would be:
index = {0, 1, 2, 3, 4, 5, 6, 7}
Now, after sorting it using Arrays.sort(Array);
newArray will be like:
newArray = {France, France, France, Italy, Italy, Spain, Spain, Spain}
and the newIndex will be:
newIndex = {0, 2, 3, 4, 7, 1, 5, 6}
The problem is: how can I find the newIndex from the input Array?
Don't sort the array to start with. Sort the index array, passing in a comparator which compares values by using them as indexes into the array. So you end up with newIndex as the result of the sort, and it's trivial to go from there to the sorted array of actual items.
Admittedly that means sorting an array of integers in a custom way - which either means using an Integer[] and the standard Java library, or a 3rd party library which has an "IntComparator" interface which can be used in conjunction with a sort(int[], IntComparator) type of method.
EDIT: Okay, here's an example comparator. For the sake of simplicity I'll assume you only want to sort an "original" array of strings... and I won't bother with nullity testing.
public class ArrayIndexComparator implements Comparator<Integer>
{
private final String[] array;
public ArrayIndexComparator(String[] array)
{
this.array = array;
}
public Integer[] createIndexArray()
{
Integer[] indexes = new Integer[array.length];
for (int i = 0; i < array.length; i++)
{
indexes[i] = i; // Autoboxing
}
return indexes;
}
#Override
public int compare(Integer index1, Integer index2)
{
// Autounbox from Integer to int to use as array indexes
return array[index1].compareTo(array[index2]);
}
}
You'd use it like this:
String[] countries = { "France", "Spain", ... };
ArrayIndexComparator comparator = new ArrayIndexComparator(countries);
Integer[] indexes = comparator.createIndexArray();
Arrays.sort(indexes, comparator);
// Now the indexes are in appropriate order.
Concise way of achieving this with Java 8 Stream API,
final String[] strArr = {"France", "Spain", "France"};
int[] sortedIndices = IntStream.range(0, strArr.length)
.boxed().sorted((i, j) -> strArr[i].compareTo(strArr[j]) )
.mapToInt(ele -> ele).toArray();
TreeMap<String,Int> map = new TreeMap<String,Int>();
for( int i : indexes ) {
map.put( stringarray[i], i );
}
Now iterator over map.values() to retrieve the indexes in sort order, and over map.keySet() to get the strings, or over map.entrySet() to get the String-index-Pairs.
If having a scenario of repeatedly sorting primitive float or int arrays with positive values, then a method like below yields much better (x3~x4) speed compared to using any comparators:
long time = System.currentTimeMillis();
for (int i = 0; i < iters; i++) {
float[] array = RandomUtils.randomFloatArray(-1, 1, 3000);
long[] valueKeyPairs = new long[array.length];
for (int j = 0; j < array.length; ++j) {
valueKeyPairs[j] = (((long) Float.floatToIntBits(array[j])) << 32) | (j & 0xffffffffL);
}
Arrays.sort(valueKeyPairs);
/**Then use this to retrieve the original value and index*/
//long l = valueKeyPairs[j];
//float value = Float.intBitsToFloat((int) (l >> 32));
//int index = (int) (l);
}
long millis = System.currentTimeMillis() - time;
I made the following based on #Skeet's code. I think it is a little more OOPie. I dunno.
public static <T extends Comparable<T>> List<Integer> sortIndex(List<T> in) {
ArrayList<Integer> index = new ArrayList<>();
for (int i = 0; i < in.size(); i++) {
index.add(i);
}
Collections.sort(index, new Comparator<Integer>() {
#Override
public int compare(Integer idx1, Integer idx2) {
return in.get(idx1).compareTo(in.get(idx2));
}
});
return index;
}
Instead of the class that implements the sorting and indexing having the Comparator code for the different objects coming in, the objects in the original array must implement the Comparable interface. It seems many objects of interest have a natural ordering and have the Comparable interface implemented already.
public static void main(String[] args) {
List<Integer> a1 = new ArrayList<>(Arrays.asList(2, 3, 9, 4, 1));
// Just pass in the list to have its indexes sorted by the natural ordering
List<Integer> idx = sortIndex(a1);
List<Double> a2 = new ArrayList<>(Arrays.asList(1.0, 5.3, 5.2, -3.1, 0.3));
idx = sortIndex(a2);
List<numBits> a3 = new ArrayList<>();
for (int i = 0; i < 10; i++) {
a3.add(new numBits(i));
}
// If you need to sort the indexes of your own object, you must implement
// the Comparable Interface.
idx = sortIndex(a3);
}
static class numBits implements Comparable<numBits> {
private int a;
public numBits(int i) {
a = i;
}
public String toString() {
return Integer.toString(a);
}
// Sort by the total number of bits in the number.
#Override
public int compareTo(numBits that) {
if (Integer.bitCount(this.a) < Integer.bitCount(that.a))
return -1;
if (Integer.bitCount(this.a) > Integer.bitCount(that.a))
return 1;
return 0;
}
}
One way you could do this is to Wrap the original index and country name into a separate Class. Then sort the Array based on the names. This way, your original indexes will be preserved.
What Comes at first Glance is Map them like that
Map <Integer, String> map = new HashMap<Integer, String>();
map.put(0, "France");
map.put(1, "Spain");
map.put(2, "France");
and then sort them by value like that and then you can know their indexes and values (key, values) just print the map
Iterator mapIterator = map.keySet().iterator();
while (mapIterator .hasNext()) {
String key = mapIterator.next().toString();
String value = map.get(key).toString();
System.out.println(key + " " + value);
}
i found solution.
List<String> a = {b, a, d, c};
List<Integer> b = {2, 1, 4, 3};
and if a sort
private void setsortb() {
List<String> beforeA = new ArrayList<>();
List<Integer> beforeB = new ArrayList<>();
beforeA.addAll(a);
beforeB.addAll(b);
a.sort();//change like this {a, b, c, d}
for(int i = 0; i < beforeA.size(); i++) {
int index = beforeA.indexOf(a.get(i));
b.set(i, beforeB.get(i));
}
}
like this
result
a = {a, b, c, d}
b = {1, 2, 3, 4}