is there an easy way to display duplicate values i...
# cfml-general
b
is there an easy way to display duplicate values in an Array object without resorting to a cfloop? I need to find and display any found duplicate values, along with the number of occurrences. specifically
b
The question doesn't really make sense. You're asking if you can inspect all the values in a collection without looking at the values in the collection
Generally speaking, that is impossible in any language
Now, you don't need to use "cfloop" specifically
There's a lot of ways to do it. personally, I'd probably combine
arrayReduce()
and
structFilter()
to do it using an intermediate struct to count the number of occurrences of each value.. They're still "looping" over the array, but it's not a cfloop proper that you're typing.
b
I need to find and display any found duplicate values, along with the number of occurrences. specifically
m
Is there any specific reason you would need to avoid a loop? You'll have to evaluate each item regardless.
b
So is that your actual question?
Because I can help you with an example of that, but I'm trying to probe what you were attempting to accomplish by avoiding a loop
b
@bdw429s Yes
b
Give me a sec and I'll give you something from trycf
❤️ 1
b
I know there could be a solution of looping over the array and evaluate each value against the previous to identify duplicates. I was thinking there was a better way to do it, that I haven't learned yet.
a
Something like this? Not thoroughly tested or even thought through (scope changed whilst I was coding, this is not good): https://trycf.com/gist/f1c540229cb9917b5a0681507b269bd2/lucee5?theme=monokai That doesn't dedupe the dupes, that said, so depends on what you want
b
The resulting struct contains all array items existing more than once along with the count of occurances.
1
a
That doesn't dedupe the dupes, that said, so depends on what you want
I'd perhaps do a
.keyArray
call on that (depending on requirements), Brad. But good answer.
👍 1
b
his requirement was to get the count of the dupes, so he'd need the keys and the values from the struct
👍 1
b
so filter() is the piece I was missing. now I'm reading up on arrayFilter()
a
Ah I had stopped reading by the time that comment came along. I was working on "display duplicate values in an Array " All points towards asking clear questions eh?
👍 2
b
@Bryan Anderson That's technically
structFilter()
(assuming you're looking at my example) because the output of the
arrayReduce()
is a struct whose keys contain each unique value int he array and the values contain the occurrence count
b
Ah, okay. that makes more sense
b
So the reduce turns the array into a struct with the counts, and the filter only keeps the strut keys who have a value > 1
❤️ 1
b
I'll do better with my initial questions.
🙌 2
b
And to be clear, there is "looping" behind the scenes for both the reduce and filter, but the nice thing about functional programming is you tell it what you want, not how to do it. The looping mechanics are abstracted
1
b
Yes, that was what I was after but didn't articulate it well, at all.
b
@bhartsfield That outputs which values are duplicated, but I also needed to know how many time each value was duplicated.
reduce() and filter() are so far the most elegant solution
1
b
findAll gets a count of each duplicate
well... len() of findAll() gets the count. FindAll() actually contains them.
apparently I'm bored
b
very nice.
the added labels for count and value, make it easier to understand
If you have an array of numbers starting at 1 and ending at 100. how could I find and display any missing numbers within the array. I'm thinking arrayMap() will be part of the solution.
b
You aren't answering interview questions are you? 😉
😆 2
✔️ 1
b
no, I am challenging myself to get better with array's
b
It sounds like you're challenging us 🙂
✔️ 1
😆 1
You could loop from 1 to 100 and run arrayFind() 100 times to see which ones are missing
But I can think of several ways to accomplish that. Why don't you give it a crack and show us what you come up with
1
a
@bdw429s it was really annoying me that one can't set/update the key & return in one expression.
But this appears to do it: https://trycf.com/gist/18cfd2fcf2324fa3babc8195a478f57e/lucee5?theme=monokai (that said, don't think it's an improvement on yours, cos it ain't as clear I think)
It amuses me to find a use for
Struct.insert
, anyhow
b
Yeah, I've run across that many times. Any variable assignment operation returns the value that was set https://trycf.com/gist/a3fadeaec83a3d153454f89d7d673767/lucee5?theme=monokai so you never get back the struct itself.
CFDocs for
structInsert()
says it returns a boolean 🤮 but I would the member function "fixed" that
a
It does.
👍 1
b
Gah, but
str.insert()
doesn't override an existing key 😕
a
look @ my example
It works. It's done. It's there. Works on CF and Lucee
you need to pass
insert
that overwrite flag
1
b
Yep. pardon my use of struct insert is a little... rusty
a
bahahahahahaha
I did exact same thing
I was trying to use
structGet
a moment ago.
There's a first for everything
b
this why I love this community so much. rarely do people stop at the first solution. refactoring or different thinking is good!
a
I felt a bit guilty not using actual tests during that refactor
Or "changing shit" as it is without tests.
😆 1
b
here is what I have for finding missing numbers in an array of 1 through 100. https://trycf.com/gist/34331126329de03569244900fe97ec23/lucee5?theme=monokai
b
Copy code
offset=0;
a.each( ( v, i, a ) => {
    if( v != i + offset ) { 
        dump( i + offset );
        offset++; 
    } 
} );
👀 1