Saturday, February 25, 2023
HomeSoftware EngineeringHow you can Type Array by Frequency in Java

How you can Type Array by Frequency in Java


The problem

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

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

The answer in Java code

Possibility 1:

import java.util.*;

public class Resolution {
  public static int[] sortByFrequency(int[] array) {
    Map<Integer,Integer> freq = new HashMap<>();
    Listing<Integer> checklist = new ArrayList<>();
    for (int a : array) {
        freq.put(a, freq.getOrDefault(a, 0) + 1);
        checklist.add(a);
    } 
    Collections.kind(checklist, 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 checklist.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 Resolution {
    public static int[] sortByFrequency(int[] array) {
        return IntStream.of(array)
                .boxed()
                .accumulate(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 Resolution {
    public static int[] sortByFrequency(int[] array) {
        System.out.println(Arrays.toString(array));
        Map<Integer, Lengthy > fr = Arrays.stream(array).boxed().accumulate(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();
    }
}

Check circumstances to validate our answer

import org.junit.Check;
import static org.junit.Assert.assertArrayEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {
    @Check
    public void basicTests() {
        assertArrayEquals(new int[]{3, 3, 3, 5, 5, 7, 7, 2, 9}, Resolution.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}, Resolution.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}, Resolution.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}, Resolution.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}, Resolution.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