'How to retrieve distinct items from Firebase database?
I'm writing an Android app and I need to retrieve all the distinct values of the field 'brand' from the Firebase Database given below. How do I do that?

Solution 1:[1]
I initiated Firebase functions and wrote a TypeScript function
db.collection('assignments').orderBy('subject') .get().then(value => { var subjects: string[] = []; value.docs.forEach(doc =>{ const thisSubject = doc.data()['subject']; console.log(thisSubject); //here's where the filtering is if (!subjects.includes(thisSubject)){ subjects += thisSubject; }; } );
That will at least get you to the array, then you can return the array to your Android app (or whatever you've initialized on Firebase).
Solution 2:[2]
If you are a Kotlin user, You may do it
fun <T> Array<out T>.distinct(): List<T>
fun ByteArray.distinct(): List<Byte>
fun ShortArray.distinct(): List<Short>
fun IntArray.distinct(): List<Int>
fun LongArray.distinct(): List<Long>
fun FloatArray.distinct(): List<Float>
fun DoubleArray.distinct(): List<Double>
fun BooleanArray.distinct(): List<Boolean>
fun CharArray.distinct(): List<Char>
reference : https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/distinct.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jhbiggs |
| Solution 2 | Muslehuddin Juned |
