Tuesday, November 28, 2023
HomeSoftware EngineeringHow one can Type Array by Frequency in Java

How one can Type Array by Frequency in Java


The problem

Type parts in an array by reducing frequency of parts. If two parts have the identical frequency, type them by growing worth.

Answer.sortByFrequency(new int[]{2, 3, 5, 3, 7, 9, 5, 3, 7});
// Returns {3, 3, 3, 5, 5, 7, 7, 2, 9}
// We type by highest frequency to lowest frequency.
// If two parts have similar frequency, we type by growing worth.

The answer in Java code

Possibility 1:

import java.util.*;

public class Answer {
  public static int[] sortByFrequency(int[] array) {
    Map<Integer,Integer> freq = new HashMap<>();
    Listing<Integer> listing = new ArrayList<>();
    for (int a : array) {
        freq.put(a, freq.getOrDefault(a, 0) + 1);
        listing.add(a);
    } 
    Collections.type(listing, new Comparator() {
      public int examine(Object o1, Object o2) {
          Integer i1 = (Integer)o1, i2 = (Integer)o2;
          Integer f1 = freq.get(i1), f2 = freq.get(i2);
          int f = f2.compareTo(f1);
          return f == 0 ? i1.compareTo(i2) : f;
      }});
    return listing.stream().mapToInt(i -> i).toArray();
  }
}

Possibility 2:

import java.util.*;
import java.util.perform.Perform;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Answer {
    public static int[] sortByFrequency(int[] array) {
        return IntStream.of(array)
                .boxed()
                .acquire(Collectors.groupingBy(Perform.id(), Collectors.counting()))
                .entrySet()
                .stream()
                .sorted(Comparator.comparingLong(i -> ((Map.Entry<Integer,Lengthy>)i).getValue())
                        .reversed()
                        .thenComparingLong(i -> ((Map.Entry<Integer,Lengthy>)i).getKey()))
                .flatMapToInt(i -> Stream.generate(i::getKey)
                        .restrict(i.getValue())
                        .mapToInt(Integer::intValue))
                .toArray();
    }
}

Possibility 3:

import java.util.*;
import java.util.perform.Perform;
import java.util.stream.Collectors;
public class Answer {
    public static int[] sortByFrequency(int[] array) {
        System.out.println(Arrays.toString(array));
        Map<Integer, Lengthy > fr = Arrays.stream(array).boxed().acquire(Collectors.groupingBy(Perform.id(),Collectors.counting()));
        return Arrays.stream(array).boxed().sorted(new Comparator<Integer>() {
            @Override
            public int examine(Integer o1, Integer o2) {
                int c = Integer.examine((int)(lengthy)fr.get(o2),(int)(lengthy)fr.get(o1));
                return c==0?Integer.examine(o1,o2):c;
            }
        }).mapToInt(x->x).toArray();
    }
}

Take a look at circumstances to validate our answer

import org.junit.Take a look at;
import static org.junit.Assert.assertArrayEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
    @Take a look at
    public void basicTests() {
        assertArrayEquals(new int[]{3, 3, 3, 5, 5, 7, 7, 2, 9}, Answer.sortByFrequency(new int[]{2, 3, 5, 3, 7, 9, 5, 3, 7}));
        assertArrayEquals(new int[]{1, 1, 1, 0, 0, 6, 6, 8, 8, 2, 3, 5, 9}, Answer.sortByFrequency(new int[]{1, 2, 3, 0, 5, 0, 1, 6, 8, 8, 6, 9, 1}));
        assertArrayEquals(new int[]{9, 9, 9, 9, 4, 4, 5, 5, 6, 6}, Answer.sortByFrequency(new int[]{5, 9, 6, 9, 6, 5, 9, 9, 4, 4}));
        assertArrayEquals(new int[]{1, 1, 2, 2, 3, 3, 4, 4, 5, 8}, Answer.sortByFrequency(new int[]{4, 4, 2, 5, 1, 1, 3, 3, 2, 8}));
        assertArrayEquals(new int[]{0, 0, 4, 4, 9, 9, 3, 5, 7, 8}, Answer.sortByFrequency(new int[]{4, 9, 5, 0, 7, 3, 8, 4, 9, 0}));
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments