Thursday, December 7, 2023
HomeSoftware EngineeringKind Array by Frequency in Java

Kind Array by Frequency in Java


The problem

Kind parts in an array by lowering frequency of parts. If two parts have the identical frequency, type them by rising 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 type by highest frequency to lowest frequency.
// If two parts have similar frequency, we type by rising worth.

The answer in Java code

Choice 1:

import java.util.*;

public class Resolution {
  public static int[] sortByFrequency(int[] array) {
    Map<Integer,Integer> freq = new HashMap<>();
    Checklist<Integer> record = new ArrayList<>();
    for (int a : array) {
        freq.put(a, freq.getOrDefault(a, 0) + 1);
        record.add(a);
    } 
    Collections.type(record, new Comparator() {
      public int evaluate(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 record.stream().mapToInt(i -> i).toArray();
  }
}

Choice 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()
                .acquire(Collectors.groupingBy(Perform.identification(), 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();
    }
}

Choice 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().acquire(Collectors.groupingBy(Perform.identification(),Collectors.counting()));
        return Arrays.stream(array).boxed().sorted(new Comparator<Integer>() {
            @Override
            public int evaluate(Integer o1, Integer o2) {
                int c = Integer.evaluate((int)(lengthy)fr.get(o2),(int)(lengthy)fr.get(o1));
                return c==0?Integer.evaluate(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}, 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